Reputation: 4047
i want to be able to pass a global object that i will later on be use for whatever to a jade template
lets say:
app.get("/", function(req, res){
var options = {
chGlobal : {// this is the object i want to be a global
"property1" : 1,
"property2" : 2,
"property3" : 3,
}
};
jade.renderFile(__dirname +'/tpl/main.jade', options, function (err, html) {
console.log(err, html);
if (err)
{
throw err
}
else
{
res.send(html);
}
});
});
i would like to be able to use "chGlobal " in other scripts that are loaded. as if chGlobal was defined in the global scope.
Thanks
Upvotes: 0
Views: 3299
Reputation: 6346
If you use jade as a view engine through express, like this:
app.set('views', __dirname); // this will be where your views are located.
app.set('view engine', 'jade');
You can specify local variables using res.locals.variable.
Example)
app.get("/", function(req, res){
res.locals.options = {
chGlobal : {// this is the object i want to be a global
"property1" : 1,
"property2" : 2,
"property3" : 3,
}
};
res.render('main');
});
Then in Jade, you can access options variable.
You can write a middleware to automagically append the global variables, like this:
app.get("/", registerGlobals, function(req, res) {
Then the middleware function would be:
function registerGlobals(req, res, next) {
res.locals.options = {
chGlobal : {// this is the object i want to be a global
"property1" : 1,
"property2" : 2,
"property3" : 3,
}
};
next();
}
More tutorials on how to use jade here: http://runnable.com/UTlPPF-f2W1TAAEe/render-jade-with-express-for-node-js
Upvotes: 4