Skip to content

Instantly share code, notes, and snippets.

@newz2000
Created July 6, 2013 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save newz2000/5941440 to your computer and use it in GitHub Desktop.
Save newz2000/5941440 to your computer and use it in GitHub Desktop.
site-wide context variables for Express apps
// the route is defined
app.get('/notices', ensureLoggedIn('/'), routes.notices);
// these variables should be available from the template even if not explicitly returned
user = getLoggedInUser();
activeNavigation = getCurrentPage();
// some old view
exports.notices = function (req, res) {
var notices = getNotices();
res.render('pages/notices', {notices: notices});
};
// template should be able to access all three variables
<ul class="{{activeNavigation}}">
<li>Homepage</li>
<li>Notices</li>
<li>Logout {{user.username}}</li>
</ul>
{{#notices}}
<p><strong>{{type}}:</strong> {{text}}
{{/notices}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment