Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass variables to a view in Express?

I am quite new to Express.js/Node.js and trying to pass multiple values from a route to a view so that I am able to show user specific input on the page. This is what I have in my route:

// Dashboard for site after the login
app.get('/dashboard', function(req, res) {
 res.render('dashboard.jade', {
  title: 'Dashboard',
  user: app.get('user_name')
 });
});

I have stored the user name in app.get('user_name'). If I replace it with a normal string like 'test string' than it works. But if I use this app.get function nothing is getting passed to the view.

Thanks

like image 244
dom Avatar asked Oct 06 '22 11:10

dom


1 Answers

Try using app.locals documented here: http://expressjs.com/api.html#app.locals

like image 91
Eric Avatar answered Oct 10 '22 01:10

Eric