Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Express view engine wrapper for Handlebars
JavaScript
branch: master

This branch is 130 commits behind donpark:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
support
.gitmodules
Readme.md
index.js
package.json
test.js

Readme.md

hbs

Express.js view engine wrapper for handlebars.js, an extension of Mustache template language.

Why

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.

Installation

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.

Usage

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).

Tricks

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.

layout.hbs

<html>
    <head>
        <title>{{title}}</title>
    </head>
    <body>
    {{{body}}}
    </body>
</html>

myview.hbs

{{#title}}My View{{/title}}
...

view rendering code

var options = {
    cache: true,
    compile: true,
    locals {
        ...
        title: function (context, fn) {
            context.title = fn(this);
            return "";
        }
    }
};
res.render("myview", options);
Something went wrong with that request. Please try again.