简体   繁体   English

Nunjucks没有渲染模板

[英]Nunjucks not rendering template

I have folder structure as follows: 我的文件夹结构如下:

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

Server.js looks like this: 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 looks like this: app / routes.js看起来像这样:

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

public/index.html looks like this: 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>

When I start up my node app and browse to localhost:8080, the page title is the string {{ awesome }} and the contains the string {{ awesome }} rather than the required "page-title". 当我启动我的节点应用程序并浏览到localhost:8080时,页面标题是字符串{{awesome}}并且包含字符串{{awesome}}而不是所需的“页面标题”。 How come nunjucks is not rendering the variable into the template? 为什么nunjucks没有将变量渲染到模板中?

You have to split 'public' folder. 你必须拆分'public'文件夹。 What is in public, cannot be processed by nunjucks. 什么是公开的,不能由nunjucks处理。 It's static (you declared it so). 它是静态的(你声明了)。 Put your nunjucks template in folder eg 'views' 把你的nunjucks模板放在文件夹中,例如'views'

/app
   |-routes.js
/public 
   |css/
   |img/
/views
   |- index.html
server.js

nunjucks.configure('views', ...)

如果你在windows上使用thinkjs:你的view.js应如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM