15

I ran into a very basic problem but I can't seem to find the answer to it. I am working with node.js, express and I am just trying to pass a local variable into the view like this:

 app.get('/', function(req, res){
  res.render("index", {locals: {
    title: "Blog",
    }
  });
});

My index view is equally simple:

h1= title

But for some reason, I keep getting this error as if the local variable is never passed:

 500 ReferenceError: /home/spartan/Node_Projects/test/views/index.jade:1 > 1| h1= title 2| title is not defined
> 1| h1= title
  2| title is not defined

I don't know what I am doing wrong! Here are the versions I am using:

  • Express: 3.0.0alpha1
  • node.JS: 0.6.14
  • Jade: 0.24.0

Someone please help so I can actually move on to learning node + express!

aeyang
  • 807
  • 4
  • 9
  • 18

3 Answers3

28

You should pass the variable without the locals. This is probably new in express 3.0.0

res.render("index", {title: "Blog"});
mihai
  • 37,072
  • 9
  • 60
  • 86
  • 1
    Thank you so much, this was it! I was just following an old tutorial and had no idea what was wrong.. – aeyang Apr 18 '12 at 17:00
4

h1 = title tries to evaluate it locally, the title you sent and that one is different. To understand the difference see:

- var title = 'my title' // - allows writing code
h1 = title

The one you should use is:

h1 #{title}
Mustafa
  • 10,013
  • 10
  • 70
  • 116
  • Thank you for telling me that, I understand that difference now. I was so sure that was it, but unfortunately, when I changed my view to just `h1 #{title}` it gave me the exact same error message... Any ideas? – aeyang Apr 18 '12 at 03:34
1

Here is a response that I made few hours ago to a smiliar question (+ deal with layout). It shows how to pass data when rendering. (Express 3.0.0 complient)

Community
  • 1
  • 1
Arnaud Rinquin
  • 4,296
  • 1
  • 30
  • 51