# HOW TO: Easily output alternating item styles via metaprogra

**URL:** <https://www.ruby-forum.com/t/how-to-easily-output-alternating-item-styles-via-metaprogra/108409>\
**Category:** Rails\
**Created:** [July 3, 2007, 8:43am UTC](https://www.ruby-forum.com/t/how-to-easily-output-alternating-item-styles-via-metaprogra/108409 "2007-07-03T08:43:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![blinking\_bear](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@blinking\_bear](https://www.ruby-forum.com/u/blinking_bear)\
**Post date:** [July 3, 2007, 8:43am UTC](https://www.ruby-forum.com/t/how-to-easily-output-alternating-item-styles-via-metaprogra/108409/1 "2007-07-03T08:43:17Z")

</div>

I always need to apply alternating item styles when outputting a  
collection,  
here’s what I’ve been using recently.

In the app helper I define this method:

def alternate(items,alt=false  
items.map{|i| i.class}.each { |k| k.class\_eval {attr\_accessor :alt}}  
items.each { |i| i.alt, alt = alt, !alt }  
end

When I call the partial I wrap the collection with this method:

\<%= render :partial =\> ‘story’, :collection =\> alternate(@stories) %\>

Now in the partial each object in the collection has the alt attribute  
available:

- Obviously I’m making the assumption that the objects don’t already  
have/use  
an alt attribute. Anyone solving this similarly?

---

<div class="post-metadata">

**Author:** ![blinking\_bear](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@blinking\_bear](https://www.ruby-forum.com/u/blinking_bear)\
**Post date:** [July 3, 2007, 8:53am UTC](https://www.ruby-forum.com/t/how-to-easily-output-alternating-item-styles-via-metaprogra/108409/2 "2007-07-03T08:53:17Z")

</div>

On 7/3/07, blinking bear [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Obviously I’m making the assumption that the objects don’t already  
> have/use an alt attribute. Anyone solving this similarly?
> 
> - Nathan

To be honest I’m not really following your code.

items.each { |i| i.alt, alt = alt, !alt } What is this doing?

I must be missing something because it seems that every call to alt  
should  
return false from the setup in the alternate method.

My mind went to the cycle method, but I’m not sure if that fits or not  
since  
I don’t follow your code.

Anyway the docs are

> **[ActionView::Helpers::TextHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html)**
>
> The TextHelper module provides a set of methods for filtering, formatting and transforming strings, which can reduce the amount of inline Ruby code in your views.

Cheers  
Daniel

---

<div class="post-metadata">

**Author:** ![blinking\_bear](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@blinking\_bear](https://www.ruby-forum.com/u/blinking_bear)\
**Post date:** [July 3, 2007, 9:01am UTC](https://www.ruby-forum.com/t/how-to-easily-output-alternating-item-styles-via-metaprogra/108409/3 "2007-07-03T09:01:00Z")

</div>

> \<%= render :partial =\> ‘story’, :collection =\> alternate(@stories) %\>
> 
> - "\>

seems kinda ferocious to me… you could use the partial counter to  
count even uneven objects, and then style them accordingly:

===========some.rhtml===============

\<%= render :partial =\> ‘story’, :collection =\> @stories %\>

=======\_story.rhtml=================

- works for ya…?

---

<div class="post-metadata">

**Author:** ![blinking\_bear](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@blinking\_bear](https://www.ruby-forum.com/u/blinking_bear)\
**Post date:** [July 3, 2007, 9:13am UTC](https://www.ruby-forum.com/t/how-to-easily-output-alternating-item-styles-via-metaprogra/108409/4 "2007-07-03T09:13:20Z")

</div>

Shai,  
haha - ferocious? you’re right

your solution worked (just needed another ?):

- thanks,  
nathan

---

<div class="post-metadata">

**Author:** ![blinking\_bear](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@blinking\_bear](https://www.ruby-forum.com/u/blinking_bear)\
**Post date:** [July 3, 2007, 9:21am UTC](https://www.ruby-forum.com/t/how-to-easily-output-alternating-item-styles-via-metaprogra/108409/5 "2007-07-03T09:21:22Z")

</div>

> your solution worked (just needed another ?):
> 
> -

oh yea, thanks…glad it helps 🙂

---

<div class="post-metadata">

**Author:** ![blinking\_bear](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@blinking\_bear](https://www.ruby-forum.com/u/blinking_bear)\
**Post date:** [July 3, 2007, 9:33am UTC](https://www.ruby-forum.com/t/how-to-easily-output-alternating-item-styles-via-metaprogra/108409/6 "2007-07-03T09:33:22Z")

</div>

If you just want to use another style for every odd row, I’m using  
something  
like this:

\<% @page.items.each do |item| %\>  
…

 "\> ... \<% end %\>

API:

> **[ActionView::Helpers::TextHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html)**
>
> The TextHelper module provides a set of methods for filtering, formatting and transforming strings, which can reduce the amount of inline Ruby code in your views.

Regards,  
Timo
