【问题标题】:Inline conditional statement using haml css使用haml css的内联条件语句
【发布时间】: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})

【问题讨论】:

  • 你有一个未放置的括号

标签: css haml


【解决方案1】:

先计算括号,因此当单位大于 10 时,您将“我的值”设置为类。

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

【讨论】:

  • 你可能想为它添加一个解释。
【解决方案2】:

对于像这样的简单情况,我更喜欢三元运算符:

%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

【讨论】:

    猜你喜欢
    • 2022-09-23
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 2017-05-16
    • 2019-05-04
    相关资源
    最近更新 更多