so this is my problem:
I have this render function somewhere in my express code:
function renderWithData(arrayOfEvents, res) {
res.render("index", {
locals: {
pageTitle: "Wochenplaner",
events: arrayOfEvents
}
});
}
it passes index.jade a variable called events. Now i want to parse this variable inside of jade, for example like this:
- if(locals.events[0] == true) alert("Bla");
But express is telling me that locals.events is undefined. How do i correctly pass a variable to that kinda "rendering-javascript"?
Okay, i'm a noob!
In my implementation this if-construct was cyceld through like this:
for(var i = 0; i < number; i++) {
if(locals.events[i].time == 7) alert("bla");
}
What i didn't consider was, that locals.events[i].time could be "undefined" because there simply wasn't a date at that index in that array. So i fixed this by adding the following:
if(typeof locals.events[i].time !== "undefined" && locals.events[i].time == 7) alert("bla");
The right way to access and pass an js-variable from node-code to jade is to access it through the object. If this is my object:
locals: {
pageTitle: "Wochenplaner",
events: arrayOfEvents
}
Then i would access it in Jade like this:
locals.events[index].property
Sorry for wasting your time :)