It is worth noting to those who may have encountered this issue since Express 3, that the dynamicMelpers method no longer exists.
Instead, you can use the app.locals function, which acts as an object in which you can store values or functions and then make them viewable. For example: -
// In your app.js etc. app.locals.title = "My App"; app.locals({ version: 3, somefunction: function() { return "function result"; } }); // Then in your templates (shown here using a jade template) =title =version =somefunction() // Will output My App 3 function result
If you need access to a query object to retrieve information, you can write a simple middle function and use the app.settings variable.
For example, if you use connect-flash to provide messages to your users, you can do something like this:
app.use(function(req, res, next) { app.set('error', req.flash('error')); next(); });
Which will give you access to the error message with the = settings.error parameter in your template.
These topics are covered here, albeit a little briefly: http://expressjs.com/api.html#app.locals
Update: Express 4
app.locals now a simple JavaScript object, so each property must be set one after another.
app.locals.version = 3; app.locals.somefunction = function() { return "function result"; }
res.locals provides exactly the same functionality, except that it should be used for request-specific data, not application data. A custom object or setting is a common use case.
res.locals.user = req.isAuthenticated() ? req.user : null; res.locals.userSettings = { backgroundColor: 'fff' }
tigerFinch Oct 13 '12 at 17:18 2012-10-13 17:18
source share