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