I am using express in nodejs, and am trying to keep my view rendering dry. Inside my layout, I have something like the following (I’m using jade)
body
nav.login
-if(currentUser)
="logged in information"
-else
="logged out information"
!=body
The problem is that every time I render, I’m now required to have
res.render('anything.jade',{
locals: {
currentUser: req.session.currentUser,
/*all of my other locals*/
}
});
It seems like a pain to have to go through all of my rendering calls and add that, and then do the same thing if I have to add any other locals to the layout. Is there some way to keep from having to retype ‘currentUser’ into the locals everywhere I render
I know this answer is late, but for posterity: if you’re using express, you should use the supplied dynamic helpers, which are view-accessible helper functions which take
req,resas arguments. In your case:Which means your view could now call
#{user}