cycle(first_value, *values) public

Returns a Cycle object whose to_s value cycles through items of an array every time it is called. This can be used to alternate classes for table rows:

  <%- for item in @items do -%>
    <tr class="<%= cycle("even", "odd") %>">
      ... use item ...
    </tr>
  <%- end -%>

You can use named cycles to prevent clashes in nested loops. You’ll have to reset the inner cycle, manually:

  <%- for item in @items do -%>
    <tr class="<%= cycle("even", "odd", :name => "row_class")
      <td>
        <%- for value in item.values do -%>
          <span style="color:'<%= cycle("red", "green", "blue"
                                        :name => "colors") %>'">
            item
          </span>
        <%- end -%>
        <%- reset_cycle("colors") -%>
      </td>
   </tr>
 <%- end -%>
Show source
Register or log in to add new notes.
September 11, 2008
1 thank

CSS columns

You can also use this in a partial to create blocks of content into columns without setting a fixed height. This one is two columns.

  .clear { clear: both;}
  .block { float:left;width:200px;}

  <div class="block">
        <p>Content Item</p>
  </div>
  <%= cycle("", "<div class=\"clear\"></div>") -%>