blinking bear
2007-Jul-03 06:42 UTC
HOW TO: Easily output alternating item styles via metaprogramming
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: <li class="<%= story.alt ? "alt" : "" %>"> Obviously I''m making the assumption that the objects don''t already have/use an alt attribute. Anyone solving this similarly? - Nathan --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Daniel N
2007-Jul-03 06:51 UTC
Re: HOW TO: Easily output alternating item styles via metaprogramming
On 7/3/07, blinking bear <blinkingbear-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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: > > <li class="<%= story. alt ? "alt" : "" %>"> > > > Obviously I''m making the assumption that the objects don''t already > have/use an alt attribute. Anyone solving this similarly? > > - NathanTo 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 http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M000628 Cheers Daniel --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Shai Rosenfeld
2007-Jul-03 07:01 UTC
Re: HOW TO: Easily output alternating item styles via metapr
> <%= render :partial => ''story'', :collection => alternate(@stories) %> > > <li class="<%= story.alt ? "alt" : "" %>">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================ <li class="<%= story_counter.modulo(2).nonzero? ''even'' : ''odds'' %>"> works for ya...? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
blinking bear
2007-Jul-03 07:12 UTC
Re: HOW TO: Easily output alternating item styles via metapr
Shai, haha - ferocious? you''re right your solution worked (just needed another ?): <li class="<%= story_counter.modulo(2).nonzero? *?* ''even'' : ''odds'' %>"> thanks, nathan On 7/3/07, Shai Rosenfeld <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > > > <%= render :partial => ''story'', :collection => alternate(@stories) %> > > > > <li class="<%= story.alt ? "alt" : "" %>"> > > 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================> > <li class="<%= story_counter.modulo(2).nonzero? ''even'' : ''odds'' %>"> > > > works for ya...? > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Shai Rosenfeld
2007-Jul-03 07:21 UTC
Re: HOW TO: Easily output alternating item styles via metapr
> your solution worked (just needed another ?): > > <li class="<%= story_counter.modulo(2).nonzero? *?* ''even'' : ''odds'' %>">oh yea, thanks..glad it helps :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Timo Springmann
2007-Jul-03 07:32 UTC
Re: HOW TO: Easily output alternating item styles via metaprogramming
If you just want to use another style for every odd row, I''m using something like this: <% @page.items.each do |item| %> ... <tr class="<%= cycle("even", "odd") %>"> ... <% end %> API: http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M000628 Regards, Timo --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---