【问题标题】:How to insert a block every several div with haml?如何用haml每隔几个div插入一个块?
【发布时间】:2012-04-23 14:00:05
【问题描述】:

我想每三个块插入一个div.row,以便包装三个span一起用于以下haml sn-p。

但是这段代码插入了<div class="row"></div>,而不是包装.span4

  - data.apps.applications.each_with_index do |app, index|
  - if index%3 == 0
    .row # This is the line I want to insert
    .span4

我怎么能在 haml 中做到这一点,或者在这种情况下,erb 更合适?

【问题讨论】:

  • 你需要正确的缩进才能工作

标签: html ruby haml


【解决方案1】:

我认为你想要的是这样的:

-data.apps.applications.each_slice(3) do |apps|
  .row
    -apps.each do |app|
      .span4

这使用each_sliceapps 是来自 applications 的三个项目的数组。

这需要来自applications 的三个元素的组,并为每个组添加一个row div,然后为每个元素添加一个span4 div,所以你得到的是这样的:

<div class="row">
  <div class="span4"></div>
  <div class="span4"></div>
  <div class="span4"></div>
</div>
<div class="row">
  <div class="span4"></div>
  <div class="span4"></div>
  <div class="span4"></div>
</div>

如果您没有三个元素的倍数,则最后一个组将只有一两个成员。

【讨论】:

    【解决方案2】:

    你的缩进是错误的

    - data.apps.applications.each_with_index do |app, index|
      - if index%3 == 0
        .row # This is the line I want to insert
      .span4
    

    【讨论】:

    • 据我所知,这会将每个.span4 放在.row 之外,这意味着该行不包含跨度
    猜你喜欢
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 2012-09-19
    相关资源
    最近更新 更多