我可以这样做吗
#{myVar || NULL}
Jade ?
如果myVar
为空,如何显示NULL
或其他内容
?我尝试了上面的代码片段,它根本没有显示任何内容。
最佳答案
变量声明时带有前导连字符
-myVar = null
h1= "h1= '" + myVar + "' with myVar: '" + myVar + "'"
// displays: h1= 'null' with myVar: 'null'
-myVar = 'Hello lucky boy'
h1= "h1= '" + myVar + "' with myVar: '" + myVar + "'"
// displays: h1= 'Hello lucky boy' with myVar: 'Hello lucky boy'
-anotherVar = (myVar || null)
h1= "h1= '" + anotherVar + "' with myVar: '-anotherVar = (myVar || null)'"
// h1= 'Hello lucky boy' with myVar: '-anotherVar = (myVar || null)'
-myVar = null
if myVar
h1= 'myVar has content'
else
h1= 'something else'
// displays: something else
您可以通过将内容保存在“test.jade”文件中、运行 jade test.jade
然后在浏览器中查看 test.html 来尝试这些小测试。
该网站有一些很棒的示例和一个交互式 Playground :https://naltatis.github.io/jade-syntax-docs/
玩得开心。
关于javascript - jade 调节给 var 一个默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33295782/