EJS is not a good template, as you would expect, because it is very old and inactive. However, templates for JS are not so good when it comes to passing variables around. I use the Sails framework, and what I do is just passing the variables to the mind in my route settings (if they are hardcoded) or in my controllers if they are dynamic.
You can also set default route parameters to res.locals using a policy.
For example, I have the following settings in my sail routes:
'GET /':{ controller: 'IndexController.js', action: 'displayIndexPage', title: 'Main page', }, 'GET /flowchart': { controller: 'FlowchartController.js', action: 'displayFlowchart', title: 'Flowchart' },
Then I have a viewToLocals.js policy with the following code:
module.exports = function(req, res, next) { res.locals.view = req.options.title; return next(); };
This, btw, will not override any data that I pass to the view from the controller or any other file. What I could do if I had your problem would be something like this:
//setPageParamsPolicy.js module.exports = function(req, res, next) { res.locals.popovers = { title: ('undefined') ? res.get('body').username : 'Default title', //or wherever you fetch data from icon : ('undefined') ? res.get('body').avatar : 'www.site.com/defaultAvatar.jpg' } return next(); };
Sails are built on Express, so perhaps this information will be useful. However, note that the sail policies apply to the actions of the controller, which means that if you define a simple route for viewing, it will not be applied - you must redirect it to a specific controller. However, you can put this object in themseleves routes:
//routes.js 'GET /userpanel': { view: 'userpanel', locals = { popover: { title: ('undefined') ? res.get('body').username : 'Default title', //or wherever you fetch data from icon : ('undefined') ? res.get('body').avatar : 'www.site.com/defaultAvatar.jpg' } } }
I also asked this question and came to the conclusion that there is no easy way to do this. But for me, Sails provides a relatively simple solution that is also relatively reliable.