javascript - jade 调节给 var 一个默认值

标签 javascript node.js pug

我可以这样做吗

#{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/

相关文章:

javascript - 如何在 html 页面的其他内容上显示一个 div?

javascript - 优先级-web-sdk : Specific number of rows

javascript - 是否正在复制文件

ajax - 在 ajax 可以做的地方使用 websocket/socket.io 有什么缺点?

javascript - 在 Jade 模板中使用 !{ } 和 #{ } 插值(感叹号对象,哈希对象)

javascript - 状态代码 304(Jade、Node、Express)

javascript - 为什么在哈巴狗的简单渲染中显示 'Cannot Get/'?

javascript - node.js 中高效的闭包结构

javascript - html iframe 什么都不显示

node.js - 用于使用同步代码模拟异步 promise 的 Bluebird 模式