dotSoftwaredotDevelopmentdotCustomersdotAbout us
PushOk logoblank
bullet Home
bullet My software
bullet Support
bullet My payments
bullet My info
bullet Subscriptions
bullet Voting
bullet Contact us
news&eventsnews and events

2012-12-21 
Major update of SVNCOM version 1.7.2 are finaly released

2012-12-21 
Major update of SVN SCC plug-in - versions 1.7.2 are finaly released

Lightweight embedded Node.js database with MongoDB API.

Archive of news

Search go
PushOk Logo blank
leftArchive of newsright

Using twitter hogan mustache template engine with nodejs express

We are using combination of nodejs express and twitter hogan engine
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);




You are 9528917 visitor since 20 Jan 2003.
510 visitors today and 4 online right now.
blank left to top right blank

© Copyright by PushOk Software, 2003-2024, webmaster@pushok.com