I want to use ejs for partials and use jade for individual pages, how to use both in one nodejs & express project?
-
2I think this answer might help you: http://stackoverflow.com/a/15064438/1266006 – Elad Amsalem May 28 '13 at 04:51
-
Out of curiosity, why would you want to to do it? – esp May 28 '13 at 21:12
-
1For partials, just use handlerbars to replace variables is fast and enough, for complex pages, a powerful templating engine is needed. that depends. I 've figure it out and I will share it – Henry Leu May 29 '13 at 06:39
-
Hey @HenryLeu can you share you wisdom please? Am willing to do the exact same thing. – AmirHd Aug 09 '13 at 01:29
2 Answers
It 's easy to find the way in expressjs api docs and consolidate.js at github
Reference the express.js doc fragment below, please
app.engine(ext, callback)
Register the given template engine callback as ext By default will require() the engine based on the file extension. For example if you try to render a "foo.jade" file Express will invoke the following internally, and cache the require() on subsequent calls to increase performance.
app.engine('jade', require('jade').__express);
For engines that do not provide .__express out of the box - or if you wish to "map" a different extension to the template engine you may use this method. For example mapping the EJS template engine to ".html" files:
app.engine('html', require('ejs').renderFile);
In this case EJS provides a .renderFile() method with the same signature that Express expects: (path, options, callback), though note that it aliases this method as ejs.__express internally so if you're using ".ejs" extensions you dont need to do anything.
Some template engines do not follow this convention, the consolidate.js library was created to map all of node's popular template engines to follow this convention, thus allowing them to work seemlessly within Express.
var engines = require('consolidate');
app.engine('haml', engines.haml);
app.engine('html', engines.hogan);

- 2,184
- 4
- 22
- 34
-
2But don't I need to use `express.set('view engine', 'ext');` somewhere? – theonlygusti Sep 03 '16 at 13:54
-
@theonlygusti Can you elaborate on this? I'm trying to get this to work and not sure if a default `view engine` is required when `consolidate` is used? I'm in the docs so a pointer to the ref would be appreciated if you have one. Thanks. – tim.rohrer Feb 25 '18 at 16:14
This works for my:
- add pug and ejs as renders
index.js
const express = require('express');
const path = require('path');
const app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.set('view engine', 'ejs');
- call render with a filename.fileext.
routes/users
const express = require('express');
const router = express.Router();
router.get('/', function (req, res, next) {
res.render('users.ejs');
});
module.exports = router;
routes/about
const express = require('express');
const router = express.Router();
router.get('/', function (req, res, next) {
res.render('about.pug');
});
module.exports = router;

- 790
- 5
- 14
-
Thank you for bringing in the new name `pug`. I'm new to this stuff. Did you do any testing with `consolidate.js`? I think that is the route I need to take since I'm trying to provide a single variable to the front-end using `ejs`. – tim.rohrer Feb 25 '18 at 16:00
-
-
I looked at `consolidate.js` but best I could tell, it doesn't let me mix render approaches on a single page. Since I was already using `PUG`, I went the route of AJAX to get my data. Overkill.... – tim.rohrer Feb 26 '18 at 21:20
-
This is an updated answer, maybe the answer should be updated to this for correctness – kabuto178 Sep 21 '18 at 20:09