node.js Jade/Pug if else 条件用法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14744984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jade/Pug if else condition usage
提问by Kumar Kailash
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 manas it's answer. What could be going wrong?
我正在使用 .js 文件将日期发送到 .jade 文件Node.js。当#{date}字段为 时false,它执行 else 并打印man为答案。可能出什么问题了?
if #{date} == false
| #{date}
else
| man
回答by Mike Causer
If date is false, do you want to output the string 'man'? If yes, your if and else statements are the wrong way around...
如果 date 为 false,是否要输出字符串 'man'?如果是的话,你的 if 和 else 语句是错误的......
How about:
怎么样:
if date
= date
else
| man
or even:
甚至:
| #{date ? date : 'man'}
or simply:
或者干脆:
| #{date || 'man'}
回答by Marc
Within if expression you write plain variable names, without #{...}
在 if 表达式中,你写的是普通的变量名,没有 #{...}
if date == false
| #{date}
else
| man
回答by blamb
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,它增加了一行,但仍然比 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'}

