So this may be a case of me completely misunderstanding functionality, but I am trying to use partials in node.js so that I have a reusable, reinsertable header and footer on my various templates similar to {% extends 'something.html' %}
in django or <? includes 'something.php ?>
in php. As I understand it this is what partials are for.
So in my app.js uses this configuration to render templates:
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'));
});
and then I have this route:
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 is simply this:
hello world
and test.html is simply this:
{{>header}}
{{ value }}
I would expect that this would render:
hello world
some value
but I get an unexpected token error when I run node app.js
pointing to hello world
in my header.html as the problem.
what am I missing in configuring this so that it will work?
For partials and how to make them work I would suggest taking a look at the consolidate.js project. It's an efford to integrate multiple template engines with express 3.x