Jade/Pug if else condition usage
Asked Answered
B

3

18

I'm sending a date to a .jade file from my .js file using Node.js. When the #{date} field is false, it executes the else and print man as it's answer. What could be going wrong?

if #{date} == false
  | #{date}
else
  | man
Blythe answered 7/2, 2013 at 6:27 Comment(0)
C
30

If date is false, do you want to output the string 'man'? If yes, your if and else statements are the wrong way around...

How about:

if date
  = date
else
  | man

or even:

| #{date ? date : 'man'}

or simply:

| #{date || 'man'}
Circe answered 19/2, 2013 at 15:24 Comment(0)
L
8

Within if expression you write plain variable names, without #{...}

if date == false
  | #{date}
else
  | man
Lordosis answered 8/2, 2013 at 21:4 Comment(0)
S
2

Your statement was backwards. For the syntax, You can use this style to work:

p Date:
  if date
    | date
  else
    |  man

Its correct that you don't need the #{} within expression. I was not able to get the = to work, or other ways on the other answers.

Ternary Style

For Myself, I too was looking for the ternary operator to do this on one line. I whittled it down to this:

p Date: #{(date ? date : "man")}

Alternatively, you can use a var, which adds one more line, but is still less lines than OP:

- var myDate = (date ? date : "man")
p Date: #{myDate}

I was not able to get the following to work, as suggested in another answer.

| #{date ? date : 'man'}
Schear answered 23/6, 2017 at 3:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.