How to use underscore in jade template
-
28-10-2019 - |
Question
I want to use underscore function in jade template, like this
p= _.keys(user)
Not for client javascript, for in redering.
Through I did require 'underscore' in app.js, did not get along well. Of course it work properly in app.js.
ReferenceError: xxxxxxx _ is not defined
this is template error message. any idea?
thanks
Solution
If you are using Express.js (presumably you would be since you're using Jade) you can add underscore as a view helper.
app.helpers({
_: require("underscore")
});
UPDATE Using Express 3+, the above will no longer work, use app.locals
instead:
app.locals._ = require("underscore");
OTHER TIPS
In Express 3.x helpers were removed. Instead use middleware and res.locals
app.use(function(req, res, next){
res.locals._ = require('underscore');
next();
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow