【问题标题】:Nunjucks not rendering templateNunjucks 不渲染模板
【发布时间】:2014-11-06 19:15:23
【问题描述】:

我的文件夹结构如下:

/app
   |-routes.js
/public 
   |-index.html
server.js

Server.js 看起来像这样:

var nunjucks = require('nunjucks');
var express        = require('express');
var app            = express();
nunjucks.configure('public', { autoescape: true, express: app });
var port = process.env.PORT || 8080; 
app.use(express.static(__dirname + '/public'));
require('./app/routes')(app); // configure our routes
app.listen(port);               
exports = module.exports = app;

app/routes.js 看起来像这样:

module.exports = function(app) {
        app.get('*', function(req, res) {
            res.render('index.html', { awesome: 'page-title' }); 
        });
    };

public/index.html 看起来像这样:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <base href="/">

    <title>{{awesome}}</title>

</head>
<body>
        <h1>{{ awesome }}</h1>    
    </div>
</body>
</html>

当我启动我的节点应用程序并浏览到 localhost:8080 时,页面标题是字符串 {{ awesome }} 并且包含字符串 {{ awesome }} 而不是必需的“page-title”。 为什么 nunjucks 没有将变量渲染到模板中?

【问题讨论】:

    标签: javascript node.js express nunjucks


    【解决方案1】:

    您必须拆分“公共”文件夹。公开的东西,nunjucks 无法处理。它是静态的(您如此声明)。将您的 nunjucks 模板放在文件夹中,例如'意见'

    /app
       |-routes.js
    /public 
       |css/
       |img/
    /views
       |- index.html
    server.js
    
    nunjucks.configure('views', ...)
    

    【讨论】:

      【解决方案2】:

      如果你在 Windows 上使用 thinkjs: 你的 view.js 应该是这样的:

      export default {type: 'nunjucks',root_path: think.ROOT_PATH + '\\view',};

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-30
        • 1970-01-01
        • 1970-01-01
        • 2017-04-26
        • 2017-06-04
        • 1970-01-01
        • 2016-11-13
        • 1970-01-01
        相关资源
        最近更新 更多