in express3.x, the api talks about app.render('view', data, callback); it is supposed to do the same thing as res.render, but with a callback with the rendered data, instead of sending it as a response. Is there a way to use app.render in a route file. It's not possible since there is no app variable anywhere but app.js maybe there is some other easy way to just render a template and store it a variable. I need it to send html emails. I'm using hogan.js templates in my app.
Thanks
You can write routes and modules in a way that app gets passed when you require
module.exports = function (app) {
return {
'index': function(req, res, next) {
// app is avail here
}
}
}
and require it accordingly and pass on the app variable in app.js
var app = module.exports = express.createServer();
var validate = require('./routes/validate')(app);
This might not be a standard way, and especially the way the route file is written might be awkward. But it makes app avail. to your routes.
Another way to do this is to call the templating engine directly. For example if you use Hogan.js with consolidate, you can do this:
var hogan = require('consolidate').hogan;
hogan(path_to_template or string, rendering_values, function(err, result) {
console.log(result);
});
I find this better than using app.render, as it is more direct.