是否可以将下面的ERB代码转换为HAML?
ERB
<div class="users">
<div class="row">
<% i = 0 %>
<% @users.each do |user| %>
<span><%= @user.name %></span>
<% if i % 4 == 0 %>
</div> <!-- Close div .row -->
<div class="row"> <!-- Open new div .row -->
<% end %>
<% i += 1 %>
<% end %>
</div> <!-- Close div .row -->
</div> <!-- Close div .users -->
结果(HTML)
<div class="users">
<div class="row">
<span>User Name 1</span>
<span>User Name 2</span>
<span>User Name 3</span>
<span>User Name 4</span>
</div> <!-- Close div .row -->
<div class="row"> <!-- Open new div .row -->
<span>User Name 5</span>
<span>User Name 6</span>
<span>User Name 7</span>
<span>User Name 8</span>
</div> <!-- Close div .row -->
<div class="row"> <!-- Open new div .row -->
<span>User Name 9</span>
<span>User Name 10</span>
<span>User Name 11</span>
<span>User Name 12</span>
</div> <!-- Close div .row -->
</div> <!-- Close div .users -->
答案 0 :(得分:2)
我想你想要这个: 编辑:
.users
-@users.each_slice(4) do |slice|
.row
-slice.each do |user|
%span= user.name
答案 1 :(得分:2)
您需要使用each_slice
:
.users
- @users.each_slice(4) do |group|
.row
- group.each do |user|
%span= user.name