mustachex

mustachex makes using the mustache templating engine in express really simple

mustachex

mustachex makes using the mustache templating engine in express really simple.

npm install mustachex
npm install https://github.com/martinrue/mustachex/tarball/master

Register mustachex as a view engine by calling app.engine and passing in mustachex.express:

app.configure(function() {
  app.engine('html', mustachex.express);
  app.set('view engine', 'html');
  app.set('views', __dirname + '/views');
});

Calling res.render will then render the named mustache template from the view directory set in express:

app.get('/', function(reqres) {
  res.render('index', { data: 'value' });
});

By default, mustachex loads all partials from the subdirectory named partials (and every subdirectory below) of the express views directory. This means that partials just work without any extra configuration required:

views/partials/hello.html

<div>Hello {{name}}</div>

views/index.html

{{> hello}}

app.js

app.get('/', function(reqres) {
  res.render('index', { name: 'John' });
});

To load partials from a custom directory, call mustachex.loadPartials and pass the full path to a custom directory:

mustachex.loadPartials(__dirname + '/views/custompartials');
 
app.configure(function() {
  app.engine('html', mustachex.express);
  app.set('view engine', 'html');
  app.set('views', __dirname + '/views');
});

mustachex supports the concept of a layout template. Pass { layout: true } or { layout: 'customfile' } to res.render to specify that the template should use a layout template:

views/layout.html

<div>{{{body}}}</div>

views/index.html

Some Content

app.js

app.get('/', function(reqres) {
  res.render('index', { layout: true });
});

If you always want mustachex to use a layout file, set the layout option in your express application and you'll no longer need to specify { layout: true } in each res.render call:

app.configure(function() {
  app.engine('html', mustachex.express);
  app.set('view engine', 'html');
  app.set('views', __dirname + '/views');
  app.set('layout', true);
});
 
app.get('/', function(reqres) {
  // layout will be used 
  res.render('index');
});

Turning layout off on a per-route basis can then be achieved by passing { layout: false } to res.render.