layouts with in layouts

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.

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.

Have you been to plugin's homepage (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

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.

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.

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

Maxim Kulkin wrote: