Using javascript code in Jade views - if(variable) shows undefined instead of passing
try if(typeof info !== "undefined")
.
I'm not too sure but I think they use with
to inject variables into the scope of your view.
with({}) { if (foo) { console.log("foo"); // fails foo is not defined. }}with({}) { if (typeof foo !== "undefined") { console.log("foo"); // no error }}
Yes, most templating engines wrap the entire compiled template function in a with statement. It is possible to do otherwise by taking the variables into account when parsing the template, but I think nearly all templating engines use with().
One solution would be to always make sure "info" is always in the locals object when rendering that template. You could do this by explicitly setting info to undefined or any other falsey value.
var locals = { info: undefined, ... };