Skip to content

Adding Additional Templating Engines

Mark Birbeck edited this page Sep 7, 2016 · 2 revisions

There are many templating languages, each of which has one or more implementations (templating engines), which in turn provide a range of different methods. This adapter not only attempts to standardise these interfaces, but also endeavours to make it as straightforward as possible to add further templating engines.

Adding an Engine That Already Supports Express

The easiest templating libraries to add are those that:

  • provide Express support by way of the express() or renderFile() method;
  • have the same module name as the template file extension.

All that is required for these is to add an entry for an engine to the dependencies in package.json, and a template with the same file extension to the fixtures collection in the tests directory (see test/fixtures). This template will be picked up automatically when running tests, and its presence will cause a test to be run for the corresponding template engine.

Adding an Express-compatible Engine Named Differently to Its Language

The next most simple templating engine to support are those that:

  • provide Express support by way of the express() or renderFile() method;
  • have a different module name to the template file extension.

As described in the previous section, an entry is needed in package.json and a template must be added to the fixtures collection in the tests directory in order to trigger tests. But in addition an entry is needed in lib/setModuleDefaults.js to indicate what the real module name is. For example, to use the module hbs to provide Handlebars support, the following needs to be added to the default settings:

handlebars: {
  "module": "hbs"
}

Without this entry the generic adapter will try to load a module called handlebars.

Adding a Template Engine That Supports a render() Method

If the templating engine to add is not Express-compatible but does support a render() method then a renderFile() method will be created for it automatically. This method will load the template referred to in the path parameter and then pass it to the template engine's render() method.

If the render() method is synchronous the adapter will need to know so that an asynchronous wrapper can be added. This is done by setting the sync option in the default configuration:

haml: {
  "sync": true
}

Adding a Template Engine That Does Not Support a render() Method

If the engine doesn't support a render() method then one is created that carries out the steps described in How It Works#render.