Jade/Pug if else condition usage

不打扰是莪最后的温柔 提交于 2019-12-03 01:44:13

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

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

if date == false
  | #{date}
else
  | man

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'}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!