Skip to content

EriksRemess/express-hbs

 
 

Repository files navigation

express-hbs

Express handlebars template engine with multiple layouts, blocks and cached partials.

Fork Notice

This project is an update of the original express-hbs, focused on latest Node.js LTS and Express 5 compatibility.

All credit for the engine design and original implementation goes to the original express-hbs developers and contributors.

v2.0.0

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

v1.0.0 Breaking Changes

If you're upgrading from v0.8.4 to v1.0.0 there are some potentially breaking changes to be aware of:

  1. 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/.
  2. The file extension for partial files must now match the extension configured in extname - please see the PR

Usage

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

// Use `.hbs` for extensions and find partials in `views/partials`.
app.engine('hbs', hbs.express({
  partialsDir: path.join(__dirname, 'views/partials')
}));
app.set('view engine', 'hbs');
app.set('views', path.join(__dirname, 'views'));

Options for #express

hbs.express({
  partialsDir: "{String/Array} [Required] Path to partials templates, one or several directories",

  // OPTIONAL settings
  restrictLayoutsTo: "{String} Absolute path to a directory to restrict layout directive reading from",
  blockHelperName: "{String} Override 'block' helper name.",
  contentHelperName: "{String} Override 'contentFor' helper name.",
  defaultLayout: "{String} Absolute path to default layout template",
  extname: "{String} Extension for templates & partials, defaults to `.hbs`",
  handlebars: "{Module} Use external handlebars instead of @eriks/express-hbs dependency",
  i18n: "{Object} i18n object",
  layoutsDir: "{String} Path to layout templates",
  templateOptions: "{Object} options to pass to template()",

  // override the default compile
  onCompile: function(exhbs, source, filename) {
    var options;
    if (filename && filename.indexOf('partials') > -1) {
      options = {preventIndent: true};
    }
    return exhbs.handlebars.compile(source, options);
  }
});

Syntax

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}}

Layouts

There are three ways to use a layout, listed in precedence order

  1. 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 to layoutsDir when configured, or to the template's directory when not.

    Set restrictLayoutsTo to 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)
    
  2. As an option to render

    layout is restricted to a safe root by default: if it starts with ., it is confined to the template's directory; otherwise it is confined to layoutsDir when configured, or views when not.

    Set restrictLayoutsTo to 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 falsey Javascript value as the value of the layout property:

    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)
    
  3. Lastly, use defaultLayout if 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 performances, and stay away from infinite loops!

Helpers

Synchronous helpers

hbs.registerHelper('link', function(text, options) {
  const isSafeAttrName = (name) => /^[A-Za-z_:][A-Za-z0-9:._-]*$/.test(name);
  const attrs = [];

  for (const prop in options.hash) {
    if (!isSafeAttrName(prop)) {
      continue;
    }

    attrs.push(
      prop + '="' + hbs.Utils.escapeExpression(options.hash[prop]) + '"'
    );
  }

  const escapedText = hbs.Utils.escapeExpression(text);
  const attrText = attrs.length > 0 ? ' ' + attrs.join(' ') : '';
  return new hbs.SafeString(
    "<a" + attrText + ">" + escapedText + "</a>"
  );
});

in markup

{{{link 'barc.com' href='http://barc.com'}}}

Asynchronous helpers

hbs.registerAsyncHelper('readFile', function(filename, cb) {
  const resolvedPath = path.resolve(viewsDir, filename);
  fs.realpath(viewsDir, function(baseErr, realViewsDir) {
    if (baseErr) {
      cb(new hbs.SafeString(''));
      return;
    }

    fs.realpath(resolvedPath, function(pathErr, realResolvedPath) {
      if (pathErr) {
        cb(new hbs.SafeString(''));
        return;
      }

      const relativePath = path.relative(realViewsDir, realResolvedPath);
      if (relativePath.startsWith('..') || path.isAbsolute(relativePath)) {
        cb(new hbs.SafeString(''));
        return;
      }

      fs.readFile(realResolvedPath, 'utf8', function(err, content) {
        cb(new hbs.SafeString(err ? '' : content));
      });
    });
  });
});

in markup

{{{readFile 'tos.txt'}}}

i18n support

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: i18n,  // registers __ and __n helpers
}));
app.set('view engine', 'hbs');
app.set('views', viewsDir);

// cookies are needed
app.use(cookieParser());

// init i18n module
app.use(i18n.init);

Engine Instances

Create isolated engine instances with their own cache system and handlebars engine.

import hbs from '@eriks/express-hbs';
var instance1 = hbs.create();
var instance2 = hbs.create();

Template options

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 3 ways. When setting global template options they can be passed as config on creation of an instance, and they can also be updated used the updateTemplateOptions(templateOptions) method of an instance. To set template options for an individual request they can be set on res.locals using the helper method 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.

Example

in File app.js

// http://expressjs.com/api.html#app.locals
app.locals({
    'PROD_MODE': 'production' === app.get('env')
});

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

TypeScript declarations

Generate declaration files (.d.ts) from JSDoc annotations:

npm run types:build

Rebuild declarations from scratch:

npm run types:rebuild

Testing

Install dependencies and run:

npm install
npm test

Credits

Inspiration and code from donpark/hbs

Big thanks to all CONTRIBUTORS

License

The MIT License (MIT)

Copyright (c) 2012-2026 Barc, Inc., Ghost Foundation - Released under the MIT license.

About

Express handlebars template engine with inheritance, partials, i18n and async helpers.

Resources

License

Stars

Watchers

Forks

Contributors

Languages

  • JavaScript 98.0%
  • Handlebars 1.8%
  • Other 0.2%