Node.js + Express - How to get Mustache partials working?

express, mustache, node.js

Solution

Managed to get this working with the latest version of hogan-express.

https://github.com/vol4ok/hogan-express

All that is needed is to install hogan-express and use it as template engine on express. No hacking or tuning required.

Problem

I'm trying to get Mustache working properly with Express, and as one can guess, I'm having troubles. The following line initializes Mustache nice and clean. Variables render as expected. ``` app.register('html', require(__dirname+'/public/js/libs/mustache.js')); ``` However, the problems start to rise when partials are thrown in the mix. With Mustache, this here partial should invoke header view/partial. ``` {{> header}} ``` But alas, nothing happens. :/ Even when I offer the partial directly, Mustache fails to render it. ``` app.get('/', function(req, res) { res.render('welcome', { partials: { header: '<h1>Header</h1>' } }); }); ``` So, it seems that the partials aren't working at all. I've found one hack that get's the partials somewhat working: http://bitdrift.com/post/2376383378/using-mustache-templates-in-express After that the partials render when offered directly at render call (see above), but it still fails on rendering the partials directly to views/layouts: Layout looks something like this: ``` Behold, the header {{> header}} <p>Let's move to more important stuff...</p> ``` Header view looks something like this: ``` <h1>Header</h1> ``` Express can load the views by themselves just ok, but it doesn't know how to handle the Mustache partials...

Original source