Zebra Striping a Collection of Partials

I’m using render to display a collection of partials, where each partial
is a table row. Is it possible to zebra-stripe the table using this
method i.e. alternate table rows get a different CSS class? I can’t work
out how each row partial would know whether it was odd or even.

In the normal scheme of things I could do:

<% odd_or_even = 0
for asset in @assets
odd_or_even = 1 - odd_or_even -%>


<% end %>

However, it would be nice to replace the above with:

<%= render :partial => ‘asset’, :collection => @assets %>



John T. wrote:

  <tr class="line<%= odd_or_even %>">


Refer to the example in the “Four Days with Rails” tutorial. It does
exactly what you want using partials…
You need to use something like: <%= render_collection_of_partials
“list_stripes”, @items %>
It’s explained on Page 27 of the tutorial :slight_smile:

Hope this helps,

That’s great, thank you!

That’s even neater, thanks Kevin.

On Sunday, June 25, 2006, at 5:57 PM, John T. wrote:

That’s great, thank you!

Posted via http://www.ruby-forum.com/.

Rails mailing list
[email protected]

Also take a look at ‘cycle’.

It’s used like this…

"> ... use item ...