variables in expressjs views variables in expressjs views express express

variables in expressjs views


Have you looked at dynamichelpers?

From the website:

app.dynamicHelpers(obj)

Registers dynamic view helpers. Dynamic view helpers are simply functions which accept req, res, and are evaluated against the Server instance before a view is rendered. The return value of this function becomes the local variable it is associated with.

app.dynamicHelpers({  session: function(req, res){    return req.session;  }});

Most of the times I like to make it a function instead. This way it will be only called when you call the function in your views.


Express v2.x

app.dynamicHelpers({  session: function(req, res){    return req.session;  }});

Express v.3

app.use(function(req, res) {  res.locals.session = req.session;});