cycle
cycle(first_value, *values)
public
Creates a Cycle object whose to_s method cycles through elements of an array every time it is called. This can be used for example, to alternate classes for table rows:
<% @items.each do |item| %> <tr class="<%= cycle("even", "odd") -%>"> <td>item</td> </tr> <% end %>
You can use named cycles to allow nesting in loops. Passing a Hash as the last parameter with a :name key will create a named cycle. You can manually reset a cycle by calling reset_cycle and passing the name of the cycle.
<% @items.each do |item| %> <tr class="<%= cycle("even", "odd", :name => "row_class") <td> <% item.values.each do |value| %> <span style="color:<%= cycle("red", "green", "blue", :name => "colors") -%>"> value </span> <% end %> <% reset_cycle("colors") %> </td> </tr> <% end %>
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>") -%>

