Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

haml new tr every 3 items

I have the following code (simplified to get to the point):

    - @assumptions[1].each do |ca|
      - if count % 3 == 1
        %tr
          %th
            = ca.value
      - else
        %th
          = ca.value
      - count = count + 1

I am not sure how to make this work in haml to have every 4 items create a new tag.

This is how it is outputting:

  <tr>
    <tr> 
      <th> 
        700.0
      </th> 
    </tr> 
    <th> 
      1235.0
    </th> 
    <th> 
      0.8
    </th> 
    <th> 
      650.0
    </th> 
  <tr> 
    <th> 
      1050.0
    </th> 
  </tr> 
  <th> 
    0.2
  </th> 
</tr>

This is how I would like it to output:

<tr>
  <th>
    700.0
  </th>
  <th>
    1235.0
  </th>
  <th>
    0.8
  </th>
</tr>
<tr>
  <th>
    650.0
  </th>
  <th>
    1050.0
  </th>
</tr>

I hope this makes sense.

like image 977
Toby Joiner Avatar asked Jun 14 '11 16:06

Toby Joiner


1 Answers

You can use Enumerable's each_slice method to group these into chunks of 3:

- @assumptions[1].each_slice(3) do |group|
  %tr
    - group.each do |ca|
      %th= ca.value
like image 104
Dylan Markow Avatar answered Oct 01 '22 02:10

Dylan Markow