Express handlebars template engine with multiple layouts, blocks and cached partials.
This project is an update of the original express-hbs, focused on Node.js 26+ and Express 5 compatibility.
- Original project: https://github.com/TryGhost/express-hbs
- Fork (this project): https://github.com/EriksRemess/express-hbs
- npm package name:
@eriks/express-hbs
All credit for the engine design and original implementation goes to the original express-hbs developers and contributors.
- Node.js 26.0.0 or newer.
- Express 5.
Node.js 26 is a Current release before its planned LTS transition in October 2026.
Version 2 was a rewrite and cleanup, with no known breaking changes. Lots of bugs were fixed which may have subtly changed behaviour.
Full details: https://github.com/TryGhost/express-hbs/releases/tag/2.0.0
If you're upgrading from v0.8.4 to v1.0.0 there are some potentially breaking changes to be aware of:
- Version 1 originally moved this project to Handlebars 4.0.5. The current fork now vendors a newer modernized Handlebars-derived runtime/compiler under
lib/handlebars/. - The file extension for partial files must now match the extension configured in
extname- please see the PR
To use with Express 5:
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import hbs from '@eriks/express-hbs';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const viewsDir = path.join(__dirname, 'views');
// Use `.hbs` for extensions and find partials in `views/partials`.
app.engine('hbs', hbs.express({
partialsDir: path.join(viewsDir, 'partials')
}));
app.set('view engine', 'hbs');
app.set('views', viewsDir);Common options for hbs.express():
hbs.express({
// Template and partial file extension. Defaults to `.hbs`.
extname: '.hbs',
// One or more partial directories.
partialsDir: path.join(viewsDir, 'partials'),
// One or more layout directories.
layoutsDir: path.join(viewsDir, 'layout'),
// View directory fallback for non-Express rendering.
viewsDir,
// Default layout path. Set to false to disable the default layout.
defaultLayout: path.join(viewsDir, 'layout/default.hbs'),
// Re-scan partial directories on each cache pass.
refreshPartialsManifest: false,
// Restrict declarative and programmatic layout reads to a safe root.
restrictLayoutsTo: viewsDir,
// Override helper names.
blockHelperName: 'block',
contentHelperName: 'contentFor',
// Use an external Handlebars instance.
// handlebars: externalHandlebars,
// Register `__` and `__n` helpers from an i18n object.
// i18n,
// Options passed to compiled templates.
templateOptions: {
data: {
appName: 'example'
}
},
// Override the default compiler.
onCompile(exhbs, source, filename) {
const options = filename?.includes('partials')
? { preventIndent: true }
: undefined;
return exhbs.handlebars.compile(source, options);
}
});To mark where layout should insert page
{{{body}}}
To declare a block placeholder in layout
{{{block "pageScripts"}}}
To define block content in a page
{{#contentFor "pageScripts"}}
CONTENT HERE
{{/contentFor}}
There are three ways to use a layout, listed in precedence order
-
Declarative within a page. Use handlebars comment
{{!< LAYOUT}}Declarative layouts are restricted to a safe root by default: if the path starts with
., it is confined to the template's directory; otherwise it is confined tolayoutsDirwhen configured, or to the template's directory when not.Set
restrictLayoutsToto override or tighten that root explicitly.Layout file resolution:
If path starts with '.' LAYOUT is relative to template Else If `layoutsDir` is set LAYOUT is relative to `layoutsDir` Else LAYOUT from path.resolve(dirname(template), LAYOUT) -
As an option to render
layoutis restricted to a safe root by default: if it starts with., it is confined to the template's directory; otherwise it is confined tolayoutsDirwhen configured, orviewswhen not.Set
restrictLayoutsToto override or tighten that root explicitly, including when you want to allow absolute layout paths inside a particular directory.res.render('veggies', { title: 'My favorite veggies', veggies: veggies, layout: 'layout/veggie' });
This option also allows for layout suppression (both the default layout and when specified declaratively in a page) by passing in a falsy JavaScript value as the value of the
layoutproperty:res.render('veggies', { title: 'My favorite veggies', veggies: veggies, layout: null // render without using a layout template });
Layout file resolution:
If path starts with '.' layout is relative to template Else If `layoutsDir` is set layout is relative to `layoutsDir` Else layout from path.resolve(viewsDir, layout) -
Lastly, use
defaultLayoutif specified in hbs configuration options.
Layouts can be nested: just include a declarative layout tag within any layout template to have its content included in the declared "parent" layout. Be aware that too much nesting can impact performance, and stay away from infinite loops!
Render options may include an AbortSignal. Template, layout, and partial filesystem reads and stats will observe the signal.
app.get('/page', (req, res, next) => {
res.render('page', {
title: 'Page',
signal: req.signal
}, (err, html) => {
if (err) {
next(err);
return;
}
res.send(html);
});
});hbs.registerHelper('link', (text, options) => {
const isSafeAttrName = (name) => /^[A-Za-z_:][A-Za-z0-9:._-]*$/.test(name);
const attrs = Object.entries(options.hash)
.filter(([name]) => isSafeAttrName(name))
.map(([name, value]) => `${name}="${hbs.Utils.escapeExpression(value)}"`)
.join(' ');
const escapedText = hbs.Utils.escapeExpression(text);
const attrText = attrs ? ` ${attrs}` : '';
return new hbs.SafeString(`<a${attrText}>${escapedText}</a>`);
});In markup:
{{{link 'barc.com' href='http://barc.com'}}}
import { readFile, realpath } from 'node:fs/promises';
import path from 'node:path';
async function readViewFile(filename) {
try {
const resolvedPath = path.resolve(viewsDir, filename);
const [realViewsDir, realResolvedPath] = await Promise.all([
realpath(viewsDir),
realpath(resolvedPath)
]);
const relativePath = path.relative(realViewsDir, realResolvedPath);
if (relativePath.startsWith('..') || path.isAbsolute(relativePath)) {
return '';
}
return readFile(realResolvedPath, 'utf8');
} catch {
return '';
}
}
hbs.registerAsyncHelper('readFile', async (filename) =>
new hbs.SafeString(await readViewFile(filename))
);In markup:
{{{readFile 'tos.txt'}}}
Callback-style async helpers are still supported for compatibility with the same readViewFile helper:
hbs.registerAsyncHelper('readFile', (filename, cb) => {
readViewFile(filename)
.then((content) => cb(new hbs.SafeString(content)))
.catch(() => cb(new hbs.SafeString('')));
});Express-hbs supports i18n
import cookieParser from 'cookie-parser';
import i18n from 'i18n';
// Minimal config
i18n.configure({
locales: ['en', 'fr'],
cookie: 'locale',
directory: path.join(__dirname, 'locales')
});
app.engine('hbs', hbs.express({
// ... options from above
i18n // registers __ and __n helpers
}));
app.set('view engine', 'hbs');
app.set('views', viewsDir);
// Cookies are needed.
app.use(cookieParser());
// Initialize the i18n middleware.
app.use(i18n.init);Create isolated engine instances with their own cache system and Handlebars engine.
import hbs from '@eriks/express-hbs';
const instance1 = hbs.create();
const instance2 = hbs.create();The main use case for template options is setting the handlebars data object. This creates global template variables accessible with an @ prefix.
Template options can be set in three ways. Global template options can be passed as config on creation of an instance, and they can also be updated using the updateTemplateOptions(templateOptions) method of an instance. Per-request template options can be set on res.locals using updateLocalTemplateOptions(locals, templateOptions).
Per-request local template options are sanitized before render and cannot override Handlebars runtime security controls such as prototype-access settings or allowCallsToHelperMissing.
Both of these methods have a companion method getTemplateOptions() and getLocalTemplateOptions(locals), which should be used when extending or merging the current options.
In file app.js
// http://expressjs.com/api.html#app.locals
app.locals({
PROD_MODE: app.get('env') === 'production'
});File views/layout/default.hbs
<html>
<head>
<title>{{title}}</title>
<link type="text/css" rel="stylesheet" href="/css/style.css"/>
{{{block "pageStyles"}}}
</head>
<body>
{{{body}}}
{{> scripts}}
{{#if PROD_MODE}}
{{{block 'googleAnalyticsScripts'}}}
{{/if}}
</body>
</html>File views/index.hbs
{{!< default}}
{{#contentFor 'pageStyles'}}
<style>
.clicker {
color: blue;
};
</style>
{{/contentFor}}
<h1>{{title}}</h1>
<p class="clicker">Click me!</p>To run example project
npm install
node example/app.jsGenerate declaration files (.d.ts) from JSDoc annotations:
npm run types:buildRebuild declarations from scratch:
npm run types:rebuildInstall dependencies and run:
npm install
npm run ciFor the quick test-and-lint path:
npm testInspiration and code from donpark/hbs
Big thanks to all CONTRIBUTORS
The MIT License (MIT)
Copyright (c) 2012-2026 Barc, Inc., Ghost Foundation - Released under the MIT license.