本文介绍了玉三元运算符添加元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
想知道是否有办法编写三元或更短形式的 if 语句,在满足 if 时将 'a' 元素添加到表格单元格中.
Wondering if there is a way to write a ternary, or shorter form of if statement, which adds the 'a' element to the table cell when the if is satisfied.
我试过了,但没有用:
td= foo.x ? a(href="/#{foo.x}/foobar") View : '-'
以下确实有效,但冗长且凌乱..
The following does work, but is quite long winded and untidy..
tbody
each foo in bar
tr
td= foo.name
if foo.x
td
a(href="/#{foo.x}/foobar") View
else
td -
if foo.y
td
a(href="/#{foo.y}/hello") Hello
else
td -
谢谢
推荐答案
事实证明,有一个三元运算符.但是,为了缩短您的代码,您可以做的是声明块并在 if/else 部分中使用它们.虽然这在技术上向您的代码添加了几行,但我认为这可以帮助您解决长 if/else 语句的问题.
As it turns out, there is a ternary operator. However, to shorten your code, what you could do is declare blocks and use them in your if/else sections. While this technically adds lines to your code, I think this helps you with your problem of long if/else statements.
使用您的示例:
block x_view
td
a(href="/#{foo.x}/foobar") View
block dash
td -
block y_hello
td
a(href="/#{foo.y}/hello") Hello
tbody
each foo in bar
tr
td= foo.name
if foo.x
block x_view
else
block dash
if foo.y
block y_hello
else
block dash
这篇关于玉三元运算符添加元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!