I have a problem with scoping when using locals in my Jade template. My code is…
function(req, res) {
BlogPostModel.find({}, function(e, data) {
if (e) throw e;
posts = data;
var path = __dirname + "/view/admin/blog.jade",
template = fs.readFileSync(path, "utf8"),
options = { filename: path },
fn = jade.compile(template, options),
html = fn(posts);
res.end(html);
});
The above code renders fine, but I’m having to make ‘data’ a global variable. I would rather pass ‘data’ directly into my function call. But when I do that, I get a ‘variable is not defined’ error from Jade. Can anyone tell my why ‘data’ is out of scope?
Thanks,
FBZ
So it wasn’t a scoping problem at all. I had to call my argument ‘locals’. Like this…
Seems strange that Jade accepts a global variable of any name, but a local variable must be called ‘locals’. Anyway, sorted.