Express.js Guide: The Comprehensive Book on Express.js (2014)
Different Template Engines
To use something different, make sure you install that module with NPM, preferably by adding it to package.json as well.
By default, Express.js will try to require a template engine based on the extension provided. That is when we call res.render('index.jade'); (more on this method later) with the index.jade file name, the framework is calling app.engine('jade', require('jade').__express); internally.
The app.engine() method example:
1 //decleare dependencies
2 //instantiate the app
3 //configure the app
4 app.engine('jade', require('jade').__express);
5 //define the routes
Therefore, to map file extensions to different template engines, please take a look at the app.engine() method. The callback, i.e., the second parameter to the method, must be in a special format. To ensure that the template library supports this format, we call __express on it.
Here is the list of the libraries that support Express.js without any modification (taken from the Express.js Wiki page):
· Jade – Haml-inspired template engine
· Haml.js – Haml implementation
· hbs – adapter for Handlebars.js, an extension of the Mustache.js template engine
· h4e – adapter for Hogan.js, with support for partials and layouts
· hulk-hogan – adapter for Twitter’s Hogan.js (Mustache syntax), with support for Partials
· combyne.js – A template engine that hopefully works the way you’d expect.
· swig – fast, Django-like template engine
· whiskers – small, fast, mustachioed
· Blade – HTML Template Compiler, inspired by Jade & Haml
· Haml-Coffee – Haml templates where you can write inline CoffeeScript.
· Webfiller – plain-html5 dual-side rendering, self-configuring routes, organized source tree, 100% js.
· express-hbs – Handlebars with layouts, partials and blocks for express 3 from Barc
· express3-handlebars – A Handlebars view engine for Express that doesn’t suck.
In case the template engine of your choice does not provide an __express() method, consider the consolidate library (GitHub). More on how to use it with Express.js in the Tips and Tricks part.