简体   繁体   中英

Inline conditional statement using haml css

How do you write inline if else in haml/css? Here is my working code:

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

I tried to make it inline like the below, but that doesnt work:

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

首先评估括号,因此当单位大于10时,将把“我的值”设置为类别。

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

For simple cases like this I prefer the ternary operator:

%span{class: unit > 10 ? 'my-value' : nil}

If it gets any more complicated than a simple condition I would extract it to a helper:

%span{class: unit_class(unit)}

And then in your helper file:

def unit_class(unit)
  if unit > 10
    'my-value'
  else
    'something-else'
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM