How to insert a block every several div with haml?

我的未来我决定 提交于 2019-12-20 05:35:09

问题


I want to insert a div.row every three blocks in order to wrap three span together for the following haml snippet.

But this code insert a <div class="row"></div> rather than wrap the .span4.

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

How could I do that in haml or in this case, erb is more suitable?


回答1:


I think what you want is something like this:

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

This uses each_slice. apps is an array of three items from applications.

This takes groups of three elements from applications, and for each group adds a row div and then adds a span4 div for each element, so what you get is something like this:

<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>

If you don't have a multiple of three elements, the last group will just have one or two members.




回答2:


Your indentation is wrong

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


来源:https://stackoverflow.com/questions/10282026/how-to-insert-a-block-every-several-div-with-haml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!