使用mustache.js部分快递

所以这可能是我完全误解function的一个例子,但是我正在尝试在node.js中使用partials,以便在类似于{% extends 'something.html' %}各种模板上有一个可重复使用的可重新插入的页眉和页脚。在Django或<? includes 'something.php ?> 在php中<? includes 'something.php ?> 。 据我所知,这是部分是为了什么。

所以在我的app.js使用这个configuration来呈现模板:

 var mustache = require('mustache'); var template = { compile: function (source, options) { if (typeof source == 'string') { return function(options) { options.locals = options.locals || {}; options.partials = options.partials || {}; if (options.body) // for express.js > v1.0 locals.body = options.body; return mustache.to_html( source, options.locals, options.partials); }; } else { return source; } }, render: function (template, options) { template = this.compile(template, options); return template(options); } }; // Configuration app.configure(function(){ app.register(".html", template); app.set('views', __dirname + '/views'); app.set('view options', {layout: false}); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(__dirname + '/public')); }); 

然后我有这条路线:

 var header = require("../views/header.html"); module.exports = function(app){ app.all('/test', function(req, res){ var data = { locals: {value: "some value"}, partials: {header: header} } res.render('test.html', data); }); 

header.html是这样的:

 hello world 

和test.html是这样的:

 {{>header}} {{ value }} 

我希望这会使得:

 hello world some value 

但我得到一个意外的令牌错误,当我运行node app.js指向hello world在我的header.html作为问题。

我在configuration这个东西时缺less什么,以便它能工作?

对于partials以及如何使它们工作,我会build议看看consolidate.js项目。 将多个模板引擎与Express 3.x集成是一个好消息