Express + jade: assuming the local variable is undefined (node.js + express + jade)

I implement webapp using node.js and express using the jade template engine.

The templates look great and can access helpers and dynamic helpers, but not local variables, except for the local variable "body", which is provided by the express function and is available and defined in my layout.jade.

This is 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 delete the second line (referring to the name), the template correctly displays the word "hello" on the web page. When I include the name = name, it raises a ReferenceError:

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

I believe that I follow jade and express examples specifically in relation to local variables. Am I doing something wrong, or could it be a mistake in express or jade?

+6
pug express
source share
3 answers
 app.set ('view engine', 'jade'); app.get ("/test", function (req, res) { res.render ('test', { name: "jake" }); }); 

you can do it like this.

+7
source share

Instead of =, you can use # {variable_name}. Here is an example of how I use it: This will make a page with a menu for navigation. Passing the page title each time the page loads, of course, you will need to create an app.get function for each page.

App.js

 var navigation = { home : { uri : "/", url : "index", title : "Home" }, lab : { uri : "/lab", url : "lab", title : "Lab" }, profile : { uri : "/profile", url : "profile", title : "Profile" }, timetable : { uri : "/timetable", url : "timetable", title : "Timetable" } } app.get(navigation.profile.uri, function(req, res){ //Profile res.render(navigation.profile.url, { title: navigation.profile.title, navigation: navigation }); }); 

profile.jade

 section#page-content h1#page-title #{title} p Welcome to #{title} 

layout.jade

 !!! 5 html head title= title link(rel='stylesheet', href='/stylesheets/reset.css') link(rel='stylesheet', href='/stylesheets/style.css') body header#site-header nav#site-navigation != partial("partials/navigation") section!= body footer#page-footer 
+3
source share

I think some error occurred due to a browser request on favicon.ico.

Try adding these lines to the head of layout.jade to link the icon

 link(rel='icon', href='/images/siteicon.png') 

This removed the same error as I was getting

+2
source share

All Articles