# @financial-times/dotcom-server-handlebars

> This package provides rendering for [Handlebars] templates with additional support for dynamically loading partial templates and a suite of [helper functions]. It is primarily designed to be used during the transition between [n-handlebars] and JSX render

Latest version **13.12.0** (published 2026-07-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install @financial-times/dotcom-server-handlebars
pnpm add @financial-times/dotcom-server-handlebars
yarn add @financial-times/dotcom-server-handlebars
bun add @financial-times/dotcom-server-handlebars
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 13.12.0 |
| Published | 2026-07-21 |
| First published | 2019-07-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | 18.x \|\| 20.x \|\| 22.x |
| Dependencies | 5 |
| Unpacked size | 101.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | the-ft, rowanmanning, chee, alexwilson, aendra, emmalewis, notlee, hamza.samih, robertboulton, leila.micollier, robgodfrey |

## Links

- npm: https://www.npmjs.com/package/@financial-times/dotcom-server-handlebars
- Homepage: https://github.com/Financial-Times/dotcom-page-kit/tree/HEAD/packages/dotcom-server-handlebars
- npm.io page: https://npm.io/package/@financial-times/dotcom-server-handlebars

## Dependencies (5)

- [glob](https://npm.io/package/glob.md) ^7.1.3
- [dateformat](https://npm.io/package/dateformat.md) ^3.0.3
- [handlebars](https://npm.io/package/handlebars.md) ^4.3.1
- [mixin-deep](https://npm.io/package/mixin-deep.md) ^2.0.0
- [@types/glob](https://npm.io/package/@types/glob.md) ^7.1.1

## Recent versions

- 13.12.0 (latest) — 2026-07-21
- 13.13.0-beta.2 (pre-release) — 2026-08-25
- 0.6.5 (maintenance) — 2020-04-16
- 13.11.0 — 2026-07-13
- 13.10.1-beta.2 — 2026-07-10
- 13.10.1-beta.1 — 2026-07-06
- 13.10.0 — 2026-04-27
- 13.10.0-beta.1 — 2026-04-21
- 13.9.2 — 2026-03-26
- 13.9.1 — 2026-03-25
- 13.9.1-beta.1 — 2026-03-25
- 13.9.0 — 2026-03-18
- 13.8.1 — 2026-03-10
- 13.8.1-beta.0 — 2026-03-10
- 13.8.0 — 2026-03-05
- … 196 more at https://npm.io/package/@financial-times/dotcom-server-handlebars/versions

## README

# @financial-times/dotcom-server-handlebars

This package provides rendering for [Handlebars] templates with additional support for dynamically loading partial templates and a suite of [helper functions]. It is primarily designed to be used during the transition between [n-handlebars] and JSX rendering and does not support layouts.

[Handlebars]: https://handlebarsjs.com/
[n-handlebars]: https://github.com/Financial-Times/n-handlebars
[helper functions]: src/helpers/README.md


## Getting started

This package is compatible with Node 12+ and is distributed on npm.

```sh
npm install --save @financial-times/dotcom-server-handlebars
```

It is best used [within an Express application](#usage-with-express) but can also be used as a [standalone library](#standalone-usage).


### Usage with Express

After installing the package you must register it as a [view engine] for your Express application. This will enable you to render template files with the matching file extension and send the result as a response to requests.

_Please note_ the template file extension registered with your application should be `.html` or `.hbs`.

```diff
const express = require('express')
+ const { PageKitHandlebars } = require('@financial-times/dotcom-server-handlebars')

+ const renderer = new PageKitHandlebars(options)
+ app.engine('.html', renderer.engine)
```

When using this module as a view engine Express will find the template file, decorate any data passed to it with properties from `app.locals` and `response.locals`, and automatically send the rendered result. See the Express [render documentation] for more information.

```js
app.get('/', (request, response) => {
  const data = {
    pageTitle: 'Home',
    content: 'Hello World!'
  }

  response.render('home.hbs', data)
})
```

_Please note_ that where to lookup template files can be configured using Express's [options](#options).

[view engine]: https://expressjs.com/en/guide/using-template-engines.html
[render documentation]: https://expressjs.com/en/4x/api.html#res.render
[settings]: https://expressjs.com/en/api.html#app.settings.table


### Standalone usage

This module can be used without integrating it fully into your application. This may be suitable for applications which are not built with Express or for ad-hoc template rendering needs.

```diff
+ const { PageKitHandlebars } = require('@financial-times/dotcom-server-handlebars')
+ const renderer = new PageKitHandlebars(options)
```

When using this module as a standalone library you will need to find template files, provide all data, and handle the rendered output manually.

```js
module.exports = (request, response, next) => {
  const data = {
    pageTitle: 'Home',
    content: 'Hello World!'
  }

  const view = path.resolve('./views/home.hbs')
  const html = renderer.render(view, data)

  response.send(html)
})
```


## API

### `.loadPartials()`

Load and compile all partial templates found with the `partialPaths` option. The template files found will be cached if caching is enabled. Returns an object mapping partial names to template functions.

### `.loadTemplate(templatePath: string)`

Loads and compiles the requested template file. The provided path will be resolved relative to the `rootDirectory` option if it is not absolute (beginning `/`). The template will be cached if caching is enabled. Returns a template function.

### `.render(template: string | function, templateContext: any)`

Renders the requested template file or template function with `templateContext`. Partial templates and helper functions will be provided to the render templateContext. Returns a string.

### `.renderView(templatePath: string, templateContext: any, callback: (error, html) => void)`

This method is intended to be used as a [view engine] for Express.


## Options

### `handlebars`

Provide an instance of Handlebars to use. Defaults to `require('handlebars')`.

### `helpers`

An object containing additional [helper functions] to register with Handlebars. Defaults to `{}`.

### `partials`

An object containing precompiled [partial templates] to register with Handlebars. Defaults to `{}`.

### `rootDirectory`

Current working directory from which to resolve partial template files. Defaults to `process.cwd()`.

### `partialPaths`

An object listing directories and patterns used to dynamically find and load partial template files. Defaults to:

```js
{
  './views/partials': '**/*.{hbs,html}',
  './bower_components': '*/{templates,components,partials,views}/**/*.{hbs,html}',
  './node_modules/@financial-times': '*/{templates,components,partials,views}/**/*.{hbs,html}'
}
```

### `cache`

A boolean which enables the caching of partial file lookup and compiled templates to enable reuse between render calls. This should always be enabled in production environments. Defaults to `process.env.NODE_ENV !== 'development`.

[helper functions]: http://handlebarsjs.com/builtin_helpers.html
[partial templates]: https://handlebarsjs.com/guide/partials.html
[n-express]: https://github.com/Financial-Times/n-express

---
_Source: https://npm.io/package/@financial-times/dotcom-server-handlebars · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
