したがって、これは機能を完全に誤解している可能性がありますが、node.jsでパーシャルを使用して{% extends 'something.html' %}
、djangoや<? includes 'something.php ?>
phpのようにさまざまなテンプレートに再利用可能で再挿入可能なヘッダーとフッターを持たせようとしています. 私が理解しているように、これがパーシャルの目的です。
したがって、私の app.js では、この構成を使用してテンプレートをレンダリングします。
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
しかし、 header.htmlを問題として指定して実行すると、予期しないトークン エラーが発生しますhello world
。
これが機能するように構成する際に何が欠けていますか?