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

7

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?

September answered 21/2, 2011 at 20:20 Comment(1)
seems by now (2014), we no longer get this error. Much easier to use.Christadelphian
A
109

This should work:

- if (typeof(username) !== 'undefined'){
  //-do something
-}
Allisonallissa answered 21/2, 2011 at 20:52 Comment(3)
- if (isset(username))' isset is not definedSeptember
Sorry, I got confused with PHP and Javascript. Updated my answer.Allisonallissa
Tanks a lot! This Code works: - if (typeof username !== "undefined")September
W
94

Simpler than @Chetan's method if you don't mind testing for falsy values instead of undefined values:

if locals.username
  p= username
else
  p No Username!

This works because the somewhat ironically named locals is the root object for the template.

Weatherly answered 13/6, 2012 at 12:54 Comment(5)
This gives me a compiler error : 'builtin_function_or_method' object has no attribute 'username'Hinz
@yourfriendzak - Are you sure your error is caused by this portion of your template?Weatherly
Works perfect and it's better than using inline javascript, thanks.Phototonus
I like this answer because this shows that Jade is whitespace sensitive language.Infecund
Careful this will only work if the variable was defined in the controller at the render stage: res.render('view', {username: user.name});Brandling
I
9
if 'username' in this
    p=username

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

Ideality answered 23/5, 2013 at 14:30 Comment(0)
C
6

If you know in advance you want a particular variable available, but not always used, I've started adding a "default" value to the helpers object.

app.helpers({ username: false });

This way, you can still do if (username) { without a catastrophic failure. :)

Corrida answered 13/6, 2012 at 13:45 Comment(2)
Thanks, though FYI for express 3.x this is now app.locals({ username: false });Premedical
Nice approach. Note that in Express 4.x app.locals is not a function anymore so it should be app.locals.username = false;Cohby
P
1

Even simpler with pug, the successor to jade

if msg
  p= msg
Parkinson answered 10/5, 2021 at 2:59 Comment(0)
R
0

Shouldn't 'username' be included in the locals object?

https://github.com/visionmedia/jade/tree/master/examples

Randolph answered 21/2, 2011 at 20:45 Comment(1)
Yes, but it still shouldn't return undefined if it's checked with if beforehand.Chance
B
0

Created a middleware to have the method isDefined available everywhere in my views:

module.exports = (req, res, next) => {
  res.locals.isDefined = (variable) => {
    return typeof(variable) !== 'undefined'
  };  
  next();
};
Brandling answered 24/4, 2017 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.