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 ejs
res.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.
cons.hogan
for hogan.js,cons.jade
for jade, etc. console.log(cons) for the full list of identifiers." – Ize