【问题标题】:HAML: Indenting if/else statements with common contentHAML:缩进具有共同内容的 if/else 语句
【发布时间】:2011-05-27 03:39:42
【问题描述】:

我的观点如下:

- if (condition)
  %div.truecondition
    (Ten lines of content)
- else
  %div.falsecondition
    (Ten lines of the same content)

我想将十行内容分解到 if/else 语句下面...但是如果我这样做,缩进意味着内容不会嵌套在 if/else 中指定的 div 中.我确定这是一个常见问题,我只是想知道解决方案是什么。那么...如何在将内容嵌套在 .truecondition/.falsecondition div 中的同时排除这十行?

【问题讨论】:

    标签: ruby-on-rails-3 haml


    【解决方案1】:

    你可以试试Ternary operator:

    %div{ :class => condition ? 'truecondition' : 'falsecondition' }
      (Ten lines of content)
    

    【讨论】:

      【解决方案2】:

      JCorcuera 的答案更好,但这更脏:

      %div(class="#{!!condition}condition")
        (Ten lines of content)
      

      假设您确实有名为 trueconditionfalsecondition 的 css 类,我输入这似乎不太可能。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-06
        • 2013-06-16
        • 2015-01-16
        • 2015-03-13
        • 1970-01-01
        相关资源
        最近更新 更多