Jade/Pug if else condition usage

前端 未结 3 1496
天命终不由人
天命终不由人 2021-02-05 09:05

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

3条回答
  •  情话喂你
    2021-02-05 09:52

    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'}
    

提交回复
热议问题