How to do multi level layout

  • A site has the default global layout and navigation links.

  • Within the site, it has several different sections,

  • each section has many pages, which has same sub nav links and some
    common parts.

  • how do i wrap a section page with section layout first, and then
    global layout?

  • what’s the best way to handle such multi level layout situation?

Two solutions I tried, and I don’t like either one.

  1. create a layout for each section, the layout duplicate global
    layout elements
  2. only use global layout, then render partial section_header,
    section_footer on every single page.

thanks.

thanks.
Dorren

What I do a lot is to have a /app/views/common directory

if you run render :partial => “/common/confirm_button”
it would render the file /app/views/common/_confirm_button.rhmtl.

but equally, I don’t know your layout,

but a “global.rhtml” layout,
which calls “render :partial => “section_links””

would render “_section_links.rhtml”
in the appropriate directory.

so in the users controller it’ll render /users/_section_links.rhtml
and in the product controller it does /product/_section_links.rhtml.

maybe that’s what you want.

Dorren wrote:

  • A site has the default global layout and navigation links.

  • Within the site, it has several different sections,

  • each section has many pages, which has same sub nav links and some
    common parts.

  • how do i wrap a section page with section layout first, and then
    global layout?

  • what’s the best way to handle such multi level layout situation?

Two solutions I tried, and I don’t like either one.

  1. create a layout for each section, the layout duplicate global
    layout elements
  2. only use global layout, then render partial section_header,
    section_footer on every single page.

thanks.

thanks.
Dorren

I hope my question is consistant with your thread. I have a VERY large
application with all kinds of sub-applications such as forums, diggs,
blogs, etc. and wish to split up the \application\app metaphor to be
\application\app\subprogram. Has anyone done this?
I see following your thread how you’d handle calling views, but what
about models and controllers?
I am grateful for any reply.
Thanks,
Kathy

but a “global.rhtml” layout,
which calls “render :partial => “section_links””

would render “_section_links.rhtml”
in the appropriate directory.

so in the users controller it’ll render /users/_section_links.rhtml
and in the product controller it does /product/_section_links.rhtml.

maybe that’s what you want.

This will work if all sections are very similar, and require every
section to have section_links.rhtml, which should be ok in most
situations.

through trial and error, i found out this setup is more flexible. To
use namespace in layout file, and rename layout as partial.
so I copy layouts/application.rhtml to shared/layouts/
_application.rhtml

======== shared/layouts/_application.rhtml =============

..... <%= render(:partial => "/shared/global_header") %>
<%= yield "top" %>
<%= yield %>

<%= render(:partial => “/shared/global_footer”) %>

======== layouts/product.rhtml =============
<% content_for :top do %>
product_home | hardware | software | …
<% end %>

<%= render(:partial => “/shared/layouts/application”) %>

======== layouts/support.rhtml =============
<%= render(:partial => “/shared/layouts/application”) %>

Product section wants a section navbar on top, so it just do that in
the product layout, for ProductController
Support section don’t need navbar, so it just skips it.

one drawback, namespaced part can not be cached.

Dorren

On 7/7/07, Dorren [email protected] wrote:

  • A site has the default global layout and navigation links.
  • Within the site, it has several different sections,
  • each section has many pages, which has same sub nav links and some
    common parts.

This PDF from a rails conf presentation has some interesting ideas:
http://culann.com/slides/Static_Site_Railsconf_2007_final.pdf

Some good ideas in this presentation but if I understand the OP’s
question, it’s about having multiple tiers of layout. Sort of like
(tags omitted for brevity):

<%= render :partial => yield :section_layout %>
Hi! Welcome to the chunky bacon section
<%= yield :layout %>
goodbye to the chunky bacon section

<% content_for :section_layout do %>my_section_layout<% end %>

Other stuff specific to the particular view. Note that the section layout can also be set in the controller using @content_for_section_layout.

I’m not positive this works, and you might have to “help” Rails
understand the search path for the section layouts, but it’s worth a
try.

I ran into a similar problem and came across the following discussion
about “nested layouts” which provides some good insight into the
matter:

it seems there’s also a nested-layout plugin available which I haven’t
had any experience with…

Mike

perhaps I’m misunderstanding but why not this:

http://agilewebdevelopment.com/plugins/nested_layouts

chau!
Tim