如何用haml每隔几格插入一个块? [英] How to insert a block every several div with haml?

查看:87
本文介绍了如何用haml每隔几格插入一个块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想每三个区块插入一个 div.row ,以便换行三个 span

但此代码插入< div class =row>< / div> ; 而不是包装 .span4

   -  data.apps.applications.each_with_index do | app,index | 
- if index%3 == 0
.row#这是我想要插入的行
.span4

我如何在haml中执行该操作,或者在这种情况下, erb 更适合?

解决方案

我想你想要的是这样的:

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

这使用 each_slice apps 是一个由应用程序中的三项数组。



这将从应用程序中的三个元素组合,并为每个组添加一个 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>

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

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?

解决方案

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.

这篇关于如何用haml每隔几格插入一个块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆