Q. default generation by express-generator
express --hbs projectname
in app.js
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
Builtin handlebars is using views/layout.hbs
by default as a master page.
Q.How to specify layout to use as part of the render call
If I create a new layout called other.hbs
res.render('view', { title: 'my other page', layout: 'other' });
To override this for the entire application, you can use
app.set('view options', { layout: 'other' });
Q. Handlebars expression
A handlebars expression is a {{
, some contents, followed by a }}
<h1>{{title}}</h1>
This expression means "look up the title
property in the current context".
Q. Handlebars helper
You can register a helper with the Handlebars.registerHelper
method.
Q. Handlebars helper or HTML escaping
If you don't want Handlebars to escape a value, use the "triple-stash", {{{
{{{link story}}}
In this case, link is the name of a Handlebars helper, and story is a parameter to the helper.
Handlebars will not escape a Handlebars.SafeString
. If you write a helper that generates its own HTML, you will usually want to return a new Handlebars.SafeString(result)
. When using SafeString all unknown or unsafe data should be manually escaped with theescapeExpression()
method
Q. Block expression
These block helpers are identified by a #
preceeding the helper name and require a matching closing mustache, /
, of the same name
Q. Partials
Handlebars partials allow for code reuse by creating shared templates. In order to use a partial, it must be registered via Handlebars.registerPartial.
Handlebars.registerPartial('myPartial', '{{name}}')
Calling the partial is done through the partial call syntax:
{{> myPartial }}