# Layouts with in layouts

**URL:** <https://www.ruby-forum.com/t/layouts-with-in-layouts/76903>\
**Category:** Rails\
**Created:** [November 22, 2006, 6:35am UTC](https://www.ruby-forum.com/t/layouts-with-in-layouts/76903 "2006-11-22T06:35:51Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [November 22, 2006, 6:35am UTC](https://www.ruby-forum.com/t/layouts-with-in-layouts/76903/1 "2006-11-22T06:35:51Z")

</div>

hello,

My app has a layout that is global. Accross all pages. I also wish to  
have a layout with in the global layout for on of my controllers. If i  
create a layout for this controller it takes over form the global  
layout. How can i stop this from happening so that the controller layout  
appears with in the global layout…

Thanks for your time reading this.

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [November 22, 2006, 8:08am UTC](https://www.ruby-forum.com/t/layouts-with-in-layouts/76903/2 "2006-11-22T08:08:01Z")

</div>

Doesn’t quite work that way. You have to use partials for this, or you  
just  
simply need to duplicate your layout and modify it for that controller.

A layout called “application.rhtml” will be used for all controllers. If  
you  
remove all other layouts, that one layout will be used everywhere  
automatically. If FooController needs a different layout, you then make  
a  
layout called ‘foo.rhtml’. It’s just a copy of ‘application.rhtml’  
but it  
has the new stuff in there. The downside is that you’re modifying two  
layouts.

So the usual approach is to put the additional stuff into a partial.

views/foo/\_layout.rhtml

Then in your layout (application.rhtml) put this code:

\<%=@custom\_layout %\>

Finally, in your controller, you should be able to do this:

class FooController \< ApplicationController  
before\_filter :set\_layout

private  
def set\_layout  
@custom\_layout = render :partial=\>“foo/layout”  
end

end

That’s how I’ve done it in the past… it would be cool to hear other  
suggestions for this. You could also put ‘if’ statements in your  
layout…  
like

\<% if controller\_name == “foo” %\>  
\<%=render :partial =\>“foo/layout” %\>  
\<% end %\>

But I don’t like that so much if you’ve got lots of different layouts.

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [November 22, 2006, 9:48am UTC](https://www.ruby-forum.com/t/layouts-with-in-layouts/76903/3 "2006-11-22T09:48:10Z")

</div>

Stewart \<rails-mailing-list@…\> writes:

> Thanks for your time reading this.

I use inside\_layout as found in the wiki:

[http://wiki.rubyonrails.org/rails/pages/Nested](http://wiki.rubyonrails.org/rails/pages/Nested)

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [November 22, 2006, 10:48am UTC](https://www.ruby-forum.com/t/layouts-with-in-layouts/76903/4 "2006-11-22T10:48:15Z")

</div>

On 22 November 2006 10:05, Brian H. wrote:

> So the usual approach is to put the additional stuff into a partial.  
> before\_filter :set\_layout  
> end
> 
> That’s how I’ve done it in the past… it would be cool to hear other  
> suggestions for this. You could also put ‘if’ statements in your layout…  
> like
> 
> \<% if controller\_name == “foo” %\>  
> \<%=render :partial =\>“foo/layout” %\>  
> \<% end %\>

That’s a damn ugly and unDRY way. Nested layouts are common approach in  
web  
site development and I was surprised that Rails doesn’t support it.

That’s why the first thing I did when I switched to Rails is to develop  
a  
method to make nested layouts possible. I first started to think about  
allowing array of layouts in ActionController::Base.layout call, but  
then it  
stroke me: every layout can be specific to it’s outer layout. So it  
turned to  
be just a simple (at first revision - 2 lines of code) helper to wrap  
template into another layout.

So, that’s how nested\_layouts plugin was born.

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [November 22, 2006, 12:52pm UTC](https://www.ruby-forum.com/t/layouts-with-in-layouts/76903/5 "2006-11-22T12:52:18Z")

</div>

Maxim K. wrote:

> On 22 November 2006 10:05, Brian H. wrote:
> 
> > So the usual approach is to put the additional stuff into a partial.  
> > before\_filter :set\_layout  
> > end
> > 
> > That’s how I’ve done it in the past… it would be cool to hear other  
> > suggestions for this. You could also put ‘if’ statements in your layout…  
> > like
> > 
> > \<% if controller\_name == “foo” %\>  
> > \<%=render :partial =\>“foo/layout” %\>  
> > \<% end %\>
> 
> That’s a damn ugly and unDRY way. Nested layouts are common approach in  
> web  
> site development and I was surprised that Rails doesn’t support it.
> 
> That’s why the first thing I did when I switched to Rails is to develop  
> a  
> method to make nested layouts possible. I first started to think about  
> allowing array of layouts in ActionController::Base.layout call, but  
> then it  
> stroke me: every layout can be specific to it’s outer layout. So it  
> turned to  
> be just a simple (at first revision - 2 lines of code) helper to wrap  
> template into another layout.
> 
> So, that’s how nested\_layouts plugin was born.

Great thats exacley what i need. But i cant seem to install it

svn://rubyforge.org/var/svn/nested-layouts

Is that the corrent address?

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [November 22, 2006, 2:30pm UTC](https://www.ruby-forum.com/t/layouts-with-in-layouts/76903/6 "2006-11-22T14:30:13Z")

</div>

Maxim K. wrote:

> On 22 November 2006 14:52, Stewart wrote:
> 
> > Great thats exacley what i need. But i cant seem to install it  
> > svn://rubyforge.org/var/svn/nested-layouts
> > 
> > Is that the corrent address?  
> > Have you been to plugin’s homepage  
> > ([http://nested-layouts.rubyforge.org/](http://nested-layouts.rubyforge.org/)) ?  
> > There is installation instructions.
> 
> To install it the correct command is  
> ./script/plugin install  
> svn://rubyforge.org/var/svn/nested-layouts/trunk/nested\_layouts

./script/plugin source svn://rubyforge.org/var/svn/nested-layouts/trunk

thats the command you have listed on the page… might want to update  
it.

great plugin i am really shocked that dhh has not yet addressed this.

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [November 22, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/layouts-with-in-layouts/76903/7 "2006-11-22T14:22:35Z")

</div>

On 22 November 2006 14:52, Stewart wrote:

> Great thats exacley what i need. But i cant seem to install it  
> svn://rubyforge.org/var/svn/nested-layouts
> 
> Is that the corrent address?  
> Have you been to plugin’s homepage  
> ([http://nested-layouts.rubyforge.org/](http://nested-layouts.rubyforge.org/)) ?  
> There is installation instructions.

To install it the correct command is  
./script/plugin install  
svn://rubyforge.org/var/svn/nested-layouts/trunk/nested\_layouts

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [November 22, 2006, 4:40pm UTC](https://www.ruby-forum.com/t/layouts-with-in-layouts/76903/8 "2006-11-22T16:40:53Z")

</div>

On 22 November 2006 16:30, Stewart wrote:

> > To install it the correct command is  
> > ./script/plugin install  
> > svn://rubyforge.org/var/svn/nested-layouts/trunk/nested\_layouts
> 
> ./script/plugin source svn://rubyforge.org/var/svn/nested-layouts/trunk
> 
> thats the command you have listed on the page… might want to update  
> it.  
> Have you noticed the “source” command ?  
> In fact, I just tested it (copy+paste those two commands and it worked).

Though, if you have lots of repositories registered, it takes some time  
to  
find where that plugin is located. I’ll consider changing installation  
instructions for a more shorter / faster / common version.

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [November 23, 2006, 2:46am UTC](https://www.ruby-forum.com/t/layouts-with-in-layouts/76903/9 "2006-11-23T02:46:25Z")

</div>

I like your plugin… but I don’t think that using partials is unDRY.  
The  
method I outlined was the same idea as content\_for\_layout.

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [November 22, 2006, 2:42pm UTC](https://www.ruby-forum.com/t/layouts-with-in-layouts/76903/10 "2006-11-22T14:42:52Z")

</div>

Maxim K. wrote:

> On 22 November 2006 14:52, Stewart wrote:
> 
> > Great thats exacley what i need. But i cant seem to install it  
> > svn://rubyforge.org/var/svn/nested-layouts
> > 
> > Is that the corrent address?  
> > Have you been to plugin’s homepage  
> > ([http://nested-layouts.rubyforge.org/](http://nested-layouts.rubyforge.org/)) ?  
> > There is installation instructions.
> 
> To install it the correct command is  
> ./script/plugin install  
> svn://rubyforge.org/var/svn/nested-layouts/trunk/nested\_layouts

i am still getting errors when i try and install the plug in … i thoght  
i got it work before but i dont think i have. I set all the code up to  
work but i was gettting

undefined method `inside\_layout’ for #\<#Class:0x36be9a0:0x36be970\>

so i thought that i had not installed the plugin correclty

sure enuf when i went to install the plugin i get the following

E:\RubyOnRails\rails\_apps???\script\>ruby plugin install ne  
sted\_layouts  
plugin: No such file or directory - svn ls  
svn://rubyforge.org/var/svn/nested-la  
youts/trunk/  
plugin: No such file or directory - svn ls  
svn://rubyforge.org/var/svn/nested-la  
youts/  
Plugin not found: nested\_layouts

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [November 23, 2006, 3:00am UTC](https://www.ruby-forum.com/t/layouts-with-in-layouts/76903/11 "2006-11-23T03:00:27Z")

</div>

On 11/23/06, Brian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I like your plugin… but I don’t think that using partials is unDRY. The  
> method I outlined was the same idea as content\_for\_layout.  
> I mean, having to maintain several layouts (general and more  
> specific), where specific layouts are full-copies of generic layout is  
> very unDRY.

Your solution with partials lack the ability to “wrap” the content in  
layout: you can just insert some code before (after) the content  
(actually, you can declare two “extension points” to insert code  
before AND after content. but this requires extra work and doesn’t  
reflect the idea of layouts). Also, it is hard to use another level of  
layout nesting: you need to define new “extension points” in 2nd level  
layout.

---

<div class="post-metadata">

**Author:** ![Stewart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Stewart](https://www.ruby-forum.com/u/Stewart)\
**Post date:** [November 30, 2006, 7:16am UTC](https://www.ruby-forum.com/t/layouts-with-in-layouts/76903/12 "2006-11-30T07:16:54Z")

</div>

Perhaps I don’t completely understand, but I’ve got a page of selects  
where each depends on what’s set in a previous select. Each select is  
drawn with it’s own partial and inside each partial I render the _next_  
partial if I already have the next value (if I’m revisiting the page to  
edit it, for example).

It ends up like this:

layout -\> partial -\> partial -\> partial -\> partial

and it works fine.

The key is that you can render another partial from within a partial.

David
