Layouts with in layouts

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.

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.

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

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.

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?

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/) ?
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.

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/) ?
There is installation instructions.

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

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.

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.

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/) ?
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

On 11/23/06, Brian H. [email protected] 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.

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