我的文件夹结构如下:
/app
|-routes.js
/public
|-index.html
server.jsServer.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看起来如下所示:
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时,页面标题是字符串{{ start },其中包含字符串{ node },而不是所需的“页面标题”。为什么nunjucks没有将变量呈现到模板中?
发布于 2014-11-06 21:50:26
你必须拆分“公用”文件夹。公开的东西,不能由修女来处理。它是静态的(你宣布是静态的)。将您的nunjucks模板放在文件夹中,例如“视图”
/app
|-routes.js
/public
|css/
|img/
/views
|- index.html
server.js
nunjucks.configure('views', ...)发布于 2018-03-16 11:04:51
如果在windows上使用thinkjs :您的view.js应该如下所示:
export default {type: 'nunjucks',root_path: think.ROOT_PATH + '\\view',};
https://stackoverflow.com/questions/26787606
复制相似问题