【发布时间】:2013-05-06 22:05:33
【问题描述】:
如果在 haml/css 中,你如何编写 inline if else ? 这是我的工作代码:
- if unit > 10
= value
- else
.unit
= value
我试图使它像下面这样内联,但这不起作用:
%span{class: ('my-value') if unit > 10})
【问题讨论】:
-
你有一个未放置的括号
如果在 haml/css 中,你如何编写 inline if else ? 这是我的工作代码:
- if unit > 10
= value
- else
.unit
= value
我试图使它像下面这样内联,但这不起作用:
%span{class: ('my-value') if unit > 10})
【问题讨论】:
先计算括号,因此当单位大于 10 时,您将“我的值”设置为类。
%span{class: ('my-value' if unit > 10)}
【讨论】:
对于像这样的简单情况,我更喜欢三元运算符:
%span{class: unit > 10 ? 'my-value' : nil}
如果它变得比简单条件更复杂,我会将其提取到帮助器中:
%span{class: unit_class(unit)}
然后在你的帮助文件中:
def unit_class(unit)
if unit > 10
'my-value'
else
'something-else'
end
end
【讨论】: