Anand Sankaran
2006-Nov-17 07:01 UTC
Setting different layouts for a controller based on a param
My webapp can be accessed in two different contexts. I want to be able to render the same view with two different layouts based on the context. Is there a way to dynamically set the layout of the views based on the context? ie, if I have a param value which indicates the context type, can I change the layout dynamically? Any help would be appreciated. -- anand --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Maxim Kulkin
2006-Nov-17 07:15 UTC
Re: Setting different layouts for a controller based on a param
On 11/17/06, Anand Sankaran <anand.sankaran-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > My webapp can be accessed in two different contexts. I want to be able to > render the same view with two different layouts based on the context. > > Is there a way to dynamically set the layout of the views based on the > context? ie, if I have a param value which indicates the context type, can > I change the layout dynamically? > > Any help would be appreciated.The classic (builtin) approach is to call render :layout => ''other_layout'' in your action. I would also recommend considering my nested_layouts plugin ( http://nested-layouts.rubyforge.org/) to do it in your view, e.g. class FooController < ApplicationController layout ''polymorphic'' before_filter :select_layout protected def select_layout if bar @layout = ''first'' else @layout = ''second'' end end end module FooHelper def polymorphic_layout @layout end end layouts/polymorphic.rhtml <% inside_layout polymorphic_layout do %> <%= yield %> <% end %> That''s it. The advantage is that you don''t need to specify proper layout in every action. Hope that helps. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Anand Sankaran
2006-Nov-17 14:56 UTC
Re: Setting different layouts for a controller based on a param
Maxim That is a neat plugin. Very useful. Kudos and many thanks. I can''t find an answer in the agile rails book, but I think it is easy to chain before_filters, so I can use multiple of these filters. Please correct me if I am wrong. Thanks again. On 11/16/06, Maxim Kulkin <maxim.kulkin-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On 11/17/06, Anand Sankaran <anand.sankaran-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > My webapp can be accessed in two different contexts. I want to be able > > to render the same view with two different layouts based on the context. > > > > Is there a way to dynamically set the layout of the views based on the > > context? ie, if I have a param value which indicates the context type, can > > I change the layout dynamically? > > > > Any help would be appreciated. > > > The classic (builtin) approach is to call > > render :layout => ''other_layout'' > > in your action. > I would also recommend considering my nested_layouts plugin ( > http://nested-layouts.rubyforge.org/) to do it in your view, e.g. > > class FooController < ApplicationController > layout ''polymorphic'' > > before_filter :select_layout > > protected > > def select_layout > if bar > @layout = ''first'' > else > @layout = ''second'' > end > end > end > > module FooHelper > def polymorphic_layout > @layout > end > end > > layouts/polymorphic.rhtml > > <% inside_layout polymorphic_layout do %> > <%= yield %> > <% end %> > > That''s it. The advantage is that you don''t need to specify proper layout > in every action. > > Hope that helps. > > > >-- anand Photo gallery: http://www.anands.net Photo blog: http://blog.anands.net --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Brian Hogan
2006-Nov-17 15:10 UTC
Re: Setting different layouts for a controller based on a param
You can have as many filters as you want. They''re run in the order you call them. If you return false in any of them, the rest of the filters after that one will not run. before_filter can be called in application.rb and since all controllers inherit from that, you can leverage that to your advantage. filters can apply to all actions before_filter :do_some_stuff Or some actions before_filter :public_page_stuff, :only=>[:list, :show] before_filter :admins_only, :except=>[:list, :show] On 11/17/06, Anand Sankaran <anand.sankaran-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Maxim > > That is a neat plugin. Very useful. Kudos and many thanks. > > I can''t find an answer in the agile rails book, but I think it is easy to > chain before_filters, so I can use multiple of these filters. Please > correct me if I am wrong. > > Thanks again. > > > On 11/16/06, Maxim Kulkin <maxim.kulkin-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > On 11/17/06, Anand Sankaran < anand.sankaran-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > My webapp can be accessed in two different contexts. I want to be > > > able to render the same view with two different layouts based on the > > > context. > > > > > > Is there a way to dynamically set the layout of the views based on the > > > context? ie, if I have a param value which indicates the context type, can > > > I change the layout dynamically? > > > > > > Any help would be appreciated. > > > > > > The classic (builtin) approach is to call > > > > render :layout => ''other_layout'' > > > > in your action. > > I would also recommend considering my nested_layouts plugin ( > > http://nested-layouts.rubyforge.org/) to do it in your view, e.g. > > > > class FooController < ApplicationController > > layout ''polymorphic'' > > > > before_filter :select_layout > > > > protected > > > > def select_layout > > if bar > > @layout = ''first'' > > else > > @layout = ''second'' > > end > > end > > end > > > > module FooHelper > > def polymorphic_layout > > @layout > > end > > end > > > > layouts/polymorphic.rhtml > > > > <% inside_layout polymorphic_layout do %> > > <%= yield %> > > <% end %> > > > > That''s it. The advantage is that you don''t need to specify proper layout > > in every action. > > > > Hope that helps. > > > > > > > > > -- > anand > > Photo gallery: http://www.anands.net > Photo blog: http://blog.anands.net > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Maxim Kulkin
2006-Nov-17 15:11 UTC
Re: Setting different layouts for a controller based on a param
On 17 November 2006 17:56, Anand Sankaran wrote:> I can''t find an answer in the agile rails book, but I think it is easy to > chain before_filters, so I can use multiple of these filters. Please > correct me if I am wrong.Yes, before and after filters are chained and, I believe, around filters are nested. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Anand Sankaran
2006-Nov-17 19:25 UTC
Re: Setting different layouts for a controller based on a param
Thanks Brian and Maxim. Yes, before and after filters are chained and, I believe, around filters are> nested. >-- anand --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---