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
On 11/17/06, Anand S. [email protected] 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.
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 K. [email protected] wrote:
Any help would be appreciated.
class FooController < ApplicationController
@layout = ‘second’
layouts/polymorphic.rhtml
–
anand
Photo gallery: http://www.anands.net
Photo blog: http://blog.anands.net
On 17 November 2006 17:56, Anand S. 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.
Thanks Brian and Maxim.
Yes, before and after filters are chained and, I believe, around filters
are
nested.
–
anand