Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

express+jade: provided local variable is undefined in view (node.js + express + jade)

I'm implementing a webapp using node.js and express, using the jade template engine.

Templates render fine, and can access helpers and dynamic helpers, but not local variables other than the "body" local variable, which is provided by express and is available and defined in my layout.jade.

This is some of the code:

app.set ('view engine', 'jade');

app.get ("/test", function (req, res) {  
    res.render ('test', {
        locals: { name: "jake" }
    });
});

and this is test.jade:

p hello
=name

when I remove the second line (referencing name), the template renders correctly, showing the word "hello" in the web page. When I include the =name, it throws a ReferenceError:

500 ReferenceError: Jade:2 NaN. 'p hello' NaN. '=name' name is not defined
NaN. 'p hello'
NaN. '=name'

I believe I'm following the jade and express examples exactly with respect to local variables. Am I doing something wrong, or could this be a bug in express or jade?

like image 603
Jake Avatar asked Jan 06 '11 02:01

Jake


1 Answers

app.set ('view engine', 'jade');

app.get ("/test", function (req, res) {  
    res.render ('test', {
        name: "jake"
    });
});

you can do it like this.

like image 114
arden Avatar answered Sep 22 '22 03:09

arden