Is there any way to use multiple view engines with Express + Node.js
Scenario: I had developed some transactional pages using Node.js, Express + Handlebars as view engine and MongoDB.
Now the issue is during module integration I got some of the pages which are built on Express + Jade as view engine.
Question: How to integrate pages built on Handlebars & some on Jade?
- Add both engines and consolidate.js in your package.json
-
In yourapp.js
var engines = require('consolidate');
app.engine('jade', engines.jade);
app.engine('handlebars', engines.handlebars);
More info here
Express 4.0 and up solution (until it changes again)
-
NPM install the engines you need.
// some examples npm install ejs npm install pug npm install handlebars
-
Set the engines to use in your
app.js
.app.set('view engine', 'pug'); app.set('view engine', 'ejs');
-
Render your template, be sure to set the file extension.
// forces usage of the respective render engine by setting the file extension explicitly. res.render( 'about.ejs', { title: 'About' } ); res.render( 'about.pug', { title: 'About' } );
-
Documentation for more usage examples.
- https://expressjs.com/en/guide/using-template-engines.html