如何在HAML中处理if / else这样的

时间:2016-06-15 21:54:36

标签: ruby-on-rails haml

我正在尝试在Bootstrap面板中显示内容,前两个组进入面板一,第二个进入面板二,最后两个进入第三个和最后一个面板。每个面板使用Bootstrap网格位于1/3列中。我无法理解如何在HAML中解决这个问题:

.row
  - x = 0
  / @educations is a grouped query by study_year, for each 2 study years (out of 6) 
  / I would like to display the educations that belong to it in the same panel
  - @educations.each do |study_year, education|
    - if x%2 == 0 // makes sure panel is only rendered once for every two iterations
      .col-sm-4
        .panel.panel-default
          .panel-heading= "Studyyear #{study_year.name}"
          %ul.list-group= render education
    - else
          %ul.list-group= render education // this gives nesting too deep error
    - x = x +1

减少最后一个ul.list-group的缩进会导致内容未显示在面板内。

关于如何实现这一目标的任何想法?

1 个答案:

答案 0 :(得分:0)

你能不能这样做:

row
  - x = 0
  / @educations is a grouped query by study_year, for each 2 study years (out of 6) 
  / I would like to display the educations that belong to it in the same panel
  - @educations.each do |study_year, education|
    - if x%2 == 0 // makes sure panel is only rendered once for every two iterations
      .col-sm-4
        .panel.panel-default
          .panel-heading= "Studyyear #{study_year.name}"
          %ul.list-group= render education
    - else
        .div
          %ul.list-group= render education // this gives nesting too deep error
    - x = x +1