Express.js view engine wrapper for handlebars.js, an extension of Mustache template language.
While Handlbars.js is already a Node.js module, it cannot be used directly as an Express.js view engine. This module will fill that role until Handlebars.js adds Express.js view engine support directly.
hbs can be added to an Express.js app as a Node.js module or you can use my fork of Express.js which:
- contains hbs as a submodule
- extends Express.js to support compile phase in view engine.
To set hbs as default view engine:
app.set("view engine", "hbs");
If you are using my fork of Express.js, compile phase is enabled only in production mode. To force enable compile phase in non-production mode:
var options = {
cache: true,
compile: true
};
If cache is not enabled, compilation will occur twice per render (once for view and once for layout).
Handlebars support view helper functions which comes in handy. For example, following code allow view templates to set page title which is typically rendered from layout template.
<html>
<head>
<title>{{title}}</title>
</head>
<body>
{{{body}}}
</body>
</html>
{{#title}}My View{{/title}}
...
var options = {
cache: true,
compile: true,
locals {
...
title: function (context, fn) {
context.title = fn(this);
return "";
}
}
};
res.render("myview", options);