【问题标题】:haml conditional if/else indentationhaml 条件 if/else 缩进
【发布时间】:2011-09-01 01:01:27
【问题描述】:

我有一种情况,我只想在计数大于 0 时才向 div 标签添加一个类

例子:

- @color.shades.each_with_index do |shade, index|
    - if index == 0
        #shades
    - else
        #shades.horizontalLine.second
     %h3 something
     %dl 
         %dt some
         %dd some1

在这个例子中,我希望从%h3 开始的所有内容都归入#shades 或#shades.horizontalLine.second,具体取决于if 和else 语句的评估结果。

解决方法是:

- @color.shades.each_with_index do |shade, index|
    - if index == 0
        #shades
          %h3 something
            %dl 
              %dt some
              %dd some1
    - else
        #shades.horizontalLine.second
          %h3 something
            %dl 
              %dt some
              %dd some1

但在这里我必须重复代码

我不知道如何在 Rails 中执行此操作,而无需为两个 div 重复从 %h3 开始的代码。

【问题讨论】:

    标签: ruby-on-rails haml


    【解决方案1】:

    您可以根据索引将类设置为保存类名的变量以将其干燥:

    - @color.shades.each_with_index do |shade, index|
      - shade_classes = index == 0 ? '' : 'horizontalLine second'
        #shades{ :class => shade_classes }
          %h3 something
            %dl 
              %dt some
              %dd some1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-14
      • 2010-12-17
      • 2011-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-28
      相关资源
      最近更新 更多