Hi, Coming from the world of PHP and Smarty, I was used to using Smarty''s cycle function to toggle a table row''s class in order to achieve alternating table rows. I am now beginning to learn Ruby and Rails and was looking to duplicate this functionality for an application that I am in the process of converting. I figured that creating a helper was the way to go and have successfully implemented my own cycle function. I have installed my helper in the ActionPack helpers folder thinking that I would test it out for my project and then probably submit it for inclusion in the framework. After a bit of testing, I discovered that when running Rails with FastCGI, all the ActionPack helpers get loaded when the server starts. This causes my cycle function to just keep cycling the same values over and over on all subsequent page loads. The initial fix for this was just to tell the cycle method to reset after the page was finished loading, but I think that is hacky. So now on to the question: is there a way to determine when a new request is made so that I can reset the cycle in the helper automatically? I suppose my other option is to just put the cycle method into the ApplicationHelper, but since I know this will get used for more than just one application, I think it will be best to have it included with ActionPack. Thanks for the input. -- DeLynn Berry delynnb-+9FQAZFMD0vCste6SmUHRhL4W9x8LtSr@public.gmane.org
I tried to build such a helper as well, but couldn''t find the Ruby equivalent of PHP''s static variables (so that each time the function was called, the counter would be remembered and incremented)... then the gang on IRC pointed out there is a pretty nice "Rails way" using partials. http://rails.rubyonrails.org/classes/ActionView/Partials.html#M000265 A counter is made available (eg article_counter), which you can then use to alternate the colors. Justin On 15/02/2005, at 8:44 AM, DeLynn Berry wrote:> Hi, > > Coming from the world of PHP and Smarty, I was used to using Smarty''s > cycle function to toggle a table row''s class in order to achieve > alternating table rows. > > I am now beginning to learn Ruby and Rails and was looking to duplicate > this functionality for an application that I am in the process of > converting. I figured that creating a helper was the way to go and have > successfully implemented my own cycle function. > > I have installed my helper in the ActionPack helpers folder thinking > that I would test it out for my project and then probably submit it for > inclusion in the framework. > > After a bit of testing, I discovered that when running Rails with > FastCGI, all the ActionPack helpers get loaded when the server starts. > This causes my cycle function to just keep cycling the same values over > and over on all subsequent page loads. The initial fix for this was > just > to tell the cycle method to reset after the page was finished loading, > but I think that is hacky. > > So now on to the question: is there a way to determine when a new > request is made so that I can reset the cycle in the helper > automatically? I suppose my other option is to just put the cycle > method > into the ApplicationHelper, but since I know this will get used for > more > than just one application, I think it will be best to have it included > with ActionPack. > > Thanks for the input. > > -- > DeLynn Berry > delynnb-+9FQAZFMD0vCste6SmUHRhL4W9x8LtSr@public.gmane.org > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >--- Justin French, Indent.com.au justin.french-zULN+VWqVOIpAS55Wn97og@public.gmane.org Web Application Development & Graphic Design
Another avenue to check out is the ruby Generator class (require ''generator''). For example: <ul> <% # define gen = Generator.new { |g| loop do %w(ping pong).each { |a| g.yield a } end } # use for volley in @table_tennis_match -%> <li><%= gen.next %></li> <% end -%> </ul> Pretty silly example, but hey, you get what you pay for. ;-) Steve On Tue, 15 Feb 2005 08:59:47 +1100, Justin French <justin.french-zULN+VWqVOIpAS55Wn97og@public.gmane.org> wrote:> I tried to build such a helper as well, but couldn''t find the Ruby > equivalent of PHP''s static variables (so that each time the function > was called, the counter would be remembered and incremented)... then > the gang on IRC pointed out there is a pretty nice "Rails way" using > partials. > > http://rails.rubyonrails.org/classes/ActionView/Partials.html#M000265 > > A counter is made available (eg article_counter), which you can then > use to alternate the colors. > > Justin > > > On 15/02/2005, at 8:44 AM, DeLynn Berry wrote: > > > Hi, > > > > Coming from the world of PHP and Smarty, I was used to using Smarty''s > > cycle function to toggle a table row''s class in order to achieve > > alternating table rows. > > > > I am now beginning to learn Ruby and Rails and was looking to duplicate > > this functionality for an application that I am in the process of > > converting. I figured that creating a helper was the way to go and have > > successfully implemented my own cycle function. > > > > I have installed my helper in the ActionPack helpers folder thinking > > that I would test it out for my project and then probably submit it for > > inclusion in the framework. > > > > After a bit of testing, I discovered that when running Rails with > > FastCGI, all the ActionPack helpers get loaded when the server starts. > > This causes my cycle function to just keep cycling the same values over > > and over on all subsequent page loads. The initial fix for this was > > just > > to tell the cycle method to reset after the page was finished loading, > > but I think that is hacky. > > > > So now on to the question: is there a way to determine when a new > > request is made so that I can reset the cycle in the helper > > automatically? I suppose my other option is to just put the cycle > > method > > into the ApplicationHelper, but since I know this will get used for > > more > > than just one application, I think it will be best to have it included > > with ActionPack. > > > > Thanks for the input. > > > > -- > > DeLynn Berry > > delynnb-+9FQAZFMD0vCste6SmUHRhL4W9x8LtSr@public.gmane.org > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > --- > Justin French, Indent.com.au > justin.french-zULN+VWqVOIpAS55Wn97og@public.gmane.org > Web Application Development & Graphic Design > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
I''m not sure if this is what you''re talking about, but here is how I do alternating css classes for table rows: class Array def cycle(other) other.each_with_index {|item, index| yield self[item % self.length], item} end end [true, false].cycle([0, 1,2,3,4,5,6,7,8,9]) do |a, b| puts "#{a.inspect} => #{b.inspect}" end Typical use in a template is <% %w{odd even}.cycle(@users) do |css_class, user| %> <tr class=<%= css_class.inspect %>> ... </tr> <% end %> Make sure you don''t try to do |class, user| as you''ll be bitten by a syntax error with regard to the reserved word ''class'', and the error message you''ll be given is hardly explanitory. -- Nicholas Seckar aka. Ulysses
Nicholas Seckar wrote:>I''m not sure if this is what you''re talking about, but here is how I do >alternating css classes for table rows: > >class Array > def cycle(other) > other.each_with_index {|item, index| yield self[item % self.length], item} > end >end > >[true, false].cycle([0, 1,2,3,4,5,6,7,8,9]) do |a, b| > puts "#{a.inspect} => #{b.inspect}" >end > >Typical use in a template is > ><% %w{odd even}.cycle(@users) do |css_class, user| %> > <tr class=<%= css_class.inspect %>> ... </tr> ><% end %> > >Make sure you don''t try to do |class, user| as you''ll be bitten by a syntax >error with regard to the reserved word ''class'', and the error message you''ll >be given is hardly explanitory. >What a beautiful code.
On Monday 14 February 2005 21:43, Demetrius Nunes wrote:> > What a beautiful code.Thanks for the compliment, but it should read class Array def cycle(other) other.each_with_index {|item, index| yield self[index % self.length], item} end end -- Nicholas Seckar aka. Ulysses
> Coming from the world of PHP and Smarty, I was used to using Smarty''s > cycle function to toggle a table row''s class in order to achieve > alternating table rows. > > I am now beginning to learn Ruby and Rails and was looking to > duplicate this functionality....Sounds like you''ve already found a solution you''re happy with, but you could also do this entirely through javascript. If you''re also doing client-side sorting, you''ll have to re-color your rows client-side anyway. A List Apart had a good article on it: http://www.alistapart.com/articles/zebratables/