typeoneerror
typeoneerror

Reputation: 56948

Assign layout-level parameter to all jade template views

I'm making my first Node site using Express and Jade and I'm puzzled as to how routing works. I've got a simple route for my index:

var routes = require('./routes');
app.get('/', routes.index);

and the corresponding definition of in ./routes/index.js:

exports.index = function(req, res){
  res.render('index', { title: 'Test', ua: req.headers['user-agent']})
};

If I wanted to assign the ua parameter to all my views, how would I do that? I'd like to not have to specify that in each route file, but I'm not finding the documentation for how to assign layout-level parameters so they are available in all my jade views.

Upvotes: 0

Views: 818

Answers (1)

mna
mna

Reputation: 23963

With a dynamic helper:

app.dynamicHelpers({
    ua: function (req, res) {
        return req.headers['user-agent'];
    }
});

Edit: link to relevant section of the docs

Note: dynamicHelpers are deprecated in Express 3

Upvotes: 1

Related Questions