# Zebra Striping a Collection of Partials

**URL:** <https://www.ruby-forum.com/t/zebra-striping-a-collection-of-partials/64416>\
**Category:** Rails\
**Created:** [June 25, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/zebra-striping-a-collection-of-partials/64416 "2006-06-25T16:49:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [June 25, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/zebra-striping-a-collection-of-partials/64416/1 "2006-06-25T16:49:05Z")

</div>

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 -%\>

  
\<td…  
  
\<% end %\>

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

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

Thanks,

John

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [June 25, 2006, 4:57pm UTC](https://www.ruby-forum.com/t/zebra-striping-a-collection-of-partials/64416/2 "2006-06-25T16:57:47Z")

</div>

John T. wrote:

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

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 🙂

Hope this helps,  
Cheers  
Mohit.

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [June 25, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/zebra-striping-a-collection-of-partials/64416/3 "2006-06-25T17:57:01Z")

</div>

That’s great, thank you!

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [June 25, 2006, 9:11pm UTC](https://www.ruby-forum.com/t/zebra-striping-a-collection-of-partials/64416/4 "2006-06-25T21:11:51Z")

</div>

That’s even neater, thanks Kevin.

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [June 25, 2006, 9:05pm UTC](https://www.ruby-forum.com/t/zebra-striping-a-collection-of-partials/64416/5 "2006-06-25T21:05:39Z")

</div>

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

> That’s great, thank you!
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Also take a look at ‘cycle’.

It’s used like this…

"\> ... use item ... 

\_Kevin
