Wednesday, 18 September 2013

Express Middleware to populate a Jade variable for all app.get()'s

Express Middleware to populate a Jade variable for all app.get()'s

I have a Jade file that all of my templates extend called layout.jade. In
it I want to be able to have a logout button if the user is currently
logged in (this is kept track of in req.session).
So layout.jade will have something like,
-if (loggedin)
a.navButton(href="/logout") Log Out
The route for a page would look something like,
app.get("/foo", function(req, res) {
res.render("foo", {loggedin: req.session.isValidUser});
});
The thing is, I don't want to have to populate the loggedin variable
manually in every single route. Is there a way I can use Express
middleware to automatically set some default options for the object sent
to res.render? Or is there a better method to do this?

No comments:

Post a Comment