Is there any way to use multiple view engines with Express + Node.js Is there any way to use multiple view engines with Express + Node.js express express

Is there any way to use multiple view engines with Express + Node.js


  1. Add both engines and consolidate.js in your package.json
  2. 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)

  1. NPM install the engines you need.

    // some examplesnpm install ejsnpm install pugnpm install handlebars
  2. Set the engines to use in your app.js.

    app.set('view engine', 'pug');app.set('view engine', 'ejs');
  3. 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' } );
  4. Documentation for more usage examples.


EDIT

After discussing with Amol M Kulkarni below comments, I came back and analyzed these again.

And turns out, it was fairly easier than I thought that I have to get back here and share my solution. Using consolidate, do it like this:

First do the require.

var engines = require('consolidate');

Then you can either remove or set engine and view engine...
I have tried removing all app.engine and app.set('view engine', '...'); and it did work. However, setting it other than 'html' will only work for one engine. So I just have set it to be sure.
I have set it like so:

app.engine('html', engines.swig); // take note, using 'html', not 'ejs' or 'pug'..app.set('view engine', 'html'); // also 'html' here.

And then later on when you do the app.render, just make sure it has the file extension and it will just work nicely.

res.render( 'theme.ejs', {}); // will render with ejsres.render( 'theme.pug', {}); // will render with pug

Just make sure have these engines (pug, ejs, etc..) are installed and consolidate will do the rest.


Old answer.
with relation to @Sergii answer, it did not work for me 100%.There are times when an error is raised in the templates I'm using. But with a wrong error message that says failed to look up this template in this directory.

I tried @azariah solution but still did not work.

app.set('view engine', 'pug'); // does not make sense.app.set('view engine', 'ejs'); // overriding the last .set()

what worked for me is using consolodate.js as mentioned.
Added app.set('view engine', 'pug'); as usual.
And then, in every time I will call render, I set the 'view engine'.

like so:

req.app.set('view engine', 'ejs');res.render( 'theme', theme );

My worries with this is that when more simultaneous users will visit page with different engines, not sure if this will collide and be back with the error look up that I'm having.

But I guess that the render is so fast, that it should be done by the time another req.app.set is called.