Code Warrior
Code Warrior

Reputation: 41

locals blank in Jade

I have a file which is setup to handle a file and set the title and locals variables for my jade template.

module.exports = {
handle_index : function(req, res) {
    res.render('index', {
        title: 'Home',
        locals: {name: 'User Name'}      
    });
}
};

My jade template:

!!!5
head
  title= title
body
  div 
    p The User Name:
    p #{locals.name}
    p Is Valid

The resulting HTML:

<!DOCTYPE html>
<head><title>Home</title></head>
<body>
<div>
<p>The User Name:</p>
<p></p>
<p>Is Valid</p>
</div>
</body>

The <p> tag is generated and blank. I've also tried =name and the same result except with no <p> tag at all.

I'm not sure why this isn't made available to the Jade template, however title is showing that everything appears connected correctly.

Upvotes: 3

Views: 2428

Answers (1)

Code Warrior
Code Warrior

Reputation: 41

I figured it out. Wasn't realizing everything was setup correctly, just had an object with nothing in it!

handle_index : function(req, res) {
    var local = {name : "User Name"};
    res.render('index', {
        title: 'Home',
        locals : local  
    });
}
};

With this Jade:

!!!5
head
  title= title
body
  div 
    p The User Name:
    p #{locals.name}
    p Is Valid

Did the trick. This is why coding for too long can be bad and an example of how a good nights rest will let you see the obvious things the next morning!

Upvotes: 1

Related Questions