How do I use a JSON variable as logic within a JADE template?

Sooooo, I have this JADE template:

//views/user.jade

div
-if ( typeof(user) !== undefined ) {
    span <%= id %>
-else {
    span nothing to see here
-}

This is how the page is rendered and how I pass in the 'user' variable in the logic

//routes/user.js

exports.user = function(req, res){
    res.render('pages/user', {
        user: req.user
    });
};

'<%= id %>' is passed in through a .JSON file and renders an ID number that is unrelated to the 'user' variable. I'd like to use the value of '<%= id %>' in my logic like so:

-if ( ( typeof(user) !== undefined ) && ( user.id === <%= id %> ) ) {

This of course, causes the page to break, along with my hopes of being a decent software engineer. What am I doing wrong here?

Thanks for your help!

So let me make some guesses:

  • By "passed through a JSON file" you mean sent in the HTTP request body in JSON format.

If that guess is correct, you need a few things:

  1. Make sure you have app.use(express.bodyParser()); which will parse the JSON request body and make req.body available
  2. Pass the value to the template

.

res.render('pages/user', {
    id: req.body.id,
    user: req.user
});

Then just reference id as a direct local javascript variable

-if ( ( typeof(user) !== undefined ) && ( user.id === id ) ) {