to render mustache templates. There is no ready module to plug-in hogan
into express, might be because this requires only few lines of code
which, however, is not that trivial to find. Additional trick is
handling of partials, including recursive, rendering which requires kind
of hook into hogan template class. Bellow you can find listing of code
that does the job.
var fs = require("fs");
var path = require("path");
var hogan=require('hogan');
var tmpl = {
compile: function (source, options) {
views = (options && options.settings && options.settings.views)
|| './views';
var tc = hogan.compile(source);
// we need overwrite for this specific template
// rp (RenderPartials) function to provide partial content
var orp = tc.rp;
tc.rp = function (name, context, partials, indent) {
var partial = partials[name];
if (partial==null) {
var partialFileName = views + '/' + name +
(options.extension || '.mustache')
partial = path.existsSync(partialFileName) ?
fs.readFileSync(partialFileName, "utf-8") : "";
partials[name]=hogan.compile(partial.toString());
}
return orp.call(this,name, context,partials, indent);
}
return function (options) {
var html = tc.render(options,options.partials);
if (options.body!=null) {
html = html.replace("",options.body);
}
return html;
}
}
}
And somewhere during express initialization:
app.configure(function(){
app.set('view engine', 'mustache');
app.set('view options',{layout:true});
app.register(".mustache", tmpl);