How to add odd_or_even automatically in the for loop

In every for loop in my application I do something like this

    <%  odd_or_even = 0
        for event in @events
            odd_or_even = 1 - odd_or_even
     %>

I know there are some ways around to that by having automatic javascript and other solutions. But we support a large number of browsers and can’t be sure everyone has the latest browser so we have settled with this solution.

I was wondering if there is a way for me to open the for loop functionaliyty and add this feature

  1. Every time the for loop starts initialize odd_or_even to zero.

  2. Change the value everytime the loop is executed.

  3. Make this varialbe odd_or_even visible exactly the way event is visible.

Any suggestions.

Thanks

In every for loop in my application I do something like this

       <% odd_or_even = 0
           for event in @events
               odd_or_even = 1 - odd_or_even
        %>

I know there are some ways around to that by having automatic javascript and
other solutions. But we support a large number of browsers and can't be sure
everyone has the latest browser so we have settled with this solution.

I was wondering if there is a way for me to open the for loop
functionaliyty and add this feature

1) Every time the for loop starts initialize odd_or_even to zero.
2) Change the value everytime the loop is executed.
3) Make this varialbe odd_or_even visible exactly the way event is visible.

cycle() might be what you're looking for...

http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M000518

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

Not sure if this applies to your problem, but have you looked at cycle()? I use it to alternate row colors in tables:

<% [1,2,3,4].each do |a| %>

<% CSSclassName = cycle(“even”, “odd”) %>

<%= a %> </tr

<% end %>

-Jason

Thanks.

Both the suggestions are neat.