[英]Inline conditional statement using haml css
您如何在haml / css中編寫內聯代碼? 這是我的工作代碼:
- 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.