Node.js: use underscore along with ejs to render html
You could bind _
to app.locals
.
Once set, the value of app.locals properties persist throughout the life of the application, in contrast with res.locals properties that are valid only for the lifetime of the request.
app.locals._ = _;app.get('/', function(req, res){ var data = {}; data.people = [{ name: 'john' }, { name: 'marry' }]; res.render('index', data);});
In your view:
<% _.each(people, function(person){ %> <div><%= person.name %></div><% }); %>
There is a great answer from another SO user: Access req and res inside of app.locals
See the documentation on app.locals
vs. req.locals
I just added quotes around the names 'john' and 'marry'