我可以将此ERB转换为HAML吗?

时间:2013-04-29 19:39:03

标签: ruby-on-rails haml erb

是否可以将下面的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 -->

2 个答案:

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