2013-05-06 70 views
1

如何在haml/css中編寫內聯if else? 這是我工作的代碼:使用haml css的內聯條件語句

- if unit > 10 
    = value 
- else 
    .unit 
    = value 

我試圖使它內嵌像下面,但是,這並不工作:

%span{class: ('my-value') if unit > 10}) 
+0

你有思念放在括號 – juanpastas 2013-05-06 22:08:03

回答

2

括號內先進行計算,這樣你纔會有「我的價值」集作爲類時單元在大於10

%span{class: ('my-value' if unit > 10)} 
+0

你可能想爲它添加一個解釋。 – fotanus 2013-05-06 22:35:54

+0

是的,我會的,但我不知道如何,讓我試試 – juanpastas 2013-05-06 23:02:04

3

對於簡單的情況下,這樣我更喜歡三元運算符:

%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