简体   繁体   English

使用haml CSS的内联条件语句

[英]Inline conditional statement using haml css

How do you write inline if else in haml/css? 您如何在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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