# Setting different layouts for a controller based on a param

**URL:** <https://www.ruby-forum.com/t/setting-different-layouts-for-a-controller-based-on-a-param/76279>\
**Category:** Rails\
**Created:** [November 17, 2006, 8:03am UTC](https://www.ruby-forum.com/t/setting-different-layouts-for-a-controller-based-on-a-param/76279 "2006-11-17T08:03:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sanand83](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@sanand83](https://www.ruby-forum.com/u/sanand83)\
**Post date:** [November 17, 2006, 8:03am UTC](https://www.ruby-forum.com/t/setting-different-layouts-for-a-controller-based-on-a-param/76279/1 "2006-11-17T08:03:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sanand83](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@sanand83](https://www.ruby-forum.com/u/sanand83)\
**Post date:** [November 17, 2006, 8:17am UTC](https://www.ruby-forum.com/t/setting-different-layouts-for-a-controller-based-on-a-param/76279/2 "2006-11-17T08:17:16Z")

</div>

On 11/17/06, Anand S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](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.

---

<div class="post-metadata">

**Author:** ![sanand83](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@sanand83](https://www.ruby-forum.com/u/sanand83)\
**Post date:** [November 17, 2006, 4:16pm UTC](https://www.ruby-forum.com/t/setting-different-layouts-for-a-controller-based-on-a-param/76279/3 "2006-11-17T16:16:25Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Any help would be appreciated.  
> > class FooController \< ApplicationController  
> > @layout = ‘second’  
> > layouts/polymorphic.rhtml

–  
anand

Photo gallery: [http://www.anands.net](http://www.anands.net)  
Photo blog: [http://blog.anands.net](http://blog.anands.net)

---

<div class="post-metadata">

**Author:** ![sanand83](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@sanand83](https://www.ruby-forum.com/u/sanand83)\
**Post date:** [November 17, 2006, 4:22pm UTC](https://www.ruby-forum.com/t/setting-different-layouts-for-a-controller-based-on-a-param/76279/4 "2006-11-17T16:22:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sanand83](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5ac83/32.png) [@sanand83](https://www.ruby-forum.com/u/sanand83)\
**Post date:** [November 17, 2006, 8:27pm UTC](https://www.ruby-forum.com/t/setting-different-layouts-for-a-controller-based-on-a-param/76279/5 "2006-11-17T20:27:18Z")

</div>

Thanks Brian and Maxim.

Yes, before and after filters are chained and, I believe, around filters  
are

> nested.

–  
anand
