I am using node.js with express and ejs.
I should move my site from ejs to another template engine - dust.js. I want to be able to move the templates from one template engine to another one by one. The problem I see is that both override the res.render method.
The solution I can think about is to store the res.render var render_ejs = res.render; after the ejs is required but before the dust.js is required. Then after the dust.js required - to store the new render but under a different name res.render_dust = res.render and override the render with the ejs ones again res.render = dust.render.
What is the right way to make them to live side by side?
Here's a simple example on how to use two engines side-by-side (I'm using Jade because Dust gave me some grief about require.paths):
var express = require('express');
var app = express();
app.engine('ejs', require('ejs').__express);
app.engine('jade', require('jade').__express);
app.set('views', __dirname);
app.get('/', function(req, res) {
// Render with Jade: res.render('index.jade');
// Render with EJS: res.render('index.ejs');
});
app.listen(3012);
So the trick is to use the file extension in render(), which combined with app.engine will make Express use the appropriate templating engine.
Thank you @robertklep.
Finally consolidate (node.js template engine consolidation library) solves my problem.
Here is a working example:
var http = require("http");
var express = require("express");
var app = express();
var express = require("express");
var app = express.createServer();
var dust = require('dustjs-linkedin'),
cons = require('consolidate');
app.engine('dust', cons.dust);
app.get("/dust", function(req, res) {
res.render('dust_template.dust', {header: 'DUST - TEST OK'});
});
app.get("/ejs", function(req, res) {
res.render('ejs_template.ejs', {header: 'EJS - TEST OK'});
});
app.listen(80);
The ejs_template.ejs and dust_template.dust files are in the views folder.