Alex Rivera | Logout

Jade - Template Engine: How to check if a variable exists

Asked 2011-02-21T20:20:42.493
84

I'm currently using Jade on a new project. I want to render a page and check if a certain variable is available.

app.js:

app.get('/register', function(req, res){
    res.render('register', {
        locals: {
          title: 'Register',
          text: 'Register as a user.',
        }
      });
});

register.jade:

- if (username)
p= username
- else
p No Username!

I always get the following error:

username is not defined

Any ideas on how I can fix this?

Edit
Report

1 Answer

9
if 'username' in this
    p=username

This works because res.locals is the root object in the template.

answered 2013-05-23T14:30:51.890

Your Answer