Sub-layouts?


#1

Is there a way to do a sub-layout? For example, you have an application
level layout that does things like headers, footers, menus, etc. This
works great, since these things appear on every page. However, a good
number of pages require some decoration (e.g. tabs), which wrap around
the unique content (what’s normally in @content_for_layout). It would be
great if you could use a tab layout that indicates that it should be
inserted into the application layout.

Is there a way to easily do this in Rails?

Thanks!

Jen


#2

render :partial => ‘sub_layout’ will do what you are after.

Warmest regards,
Nathan.


#3

Nathaniel S. H. Brown wrote:

render :partial => ‘sub_layout’ will do what you are after.

Thanks, Nathaniel. I thought of that, but couldn’t think of a way to do
it that wouldn’t involve calls to two partial templates (one for before,
one for after).

For example, I have an application.rhtml template that creates a top
menu and a left menu. Most of the time, the content does not include
tabs, but many pages do include tabs. The tabs would need to wrap
around my content.

I could accomplish this with two partials. I’d call the first partial at
the beginning of my view, and the second one at the very bottom of my
view (like a header and footer). This works, but it’s not the same as a
sub-layout, where I was envisioning that you could do the same thing
with a single file. There might not be such a thing – I just wanted to
check and see if there was some sub-layout functionality I just wasn’t
aware of.

Jen


#4

Assuming these templates:

page->tabs->content

I think a “render ‘content’” or “render @content” tag
in the ‘tabs’ template would do the trick, which
itself gets assigned to page’s content_for_layout. Or
perhaps it’s possible to capture render content
(render_component possibly) and assign that to page’s
content_for_layout.

csn

— jennyw removed_email_address@domain.invalid wrote:

one for after).
the first partial at
aware of.

Jen


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Yahoo! Mail - PC Magazine Editors’ Choice 2005
http://mail.yahoo.com


#5

On 11/11/05, jennyw removed_email_address@domain.invalid wrote:

I could accomplish this with two partials. I’d call the first partial at
the beginning of my view, and the second one at the very bottom of my
view (like a header and footer). This works, but it’s not the same as a
sub-layout, where I was envisioning that you could do the same thing
with a single file. There might not be such a thing – I just wanted to
check and see if there was some sub-layout functionality I just wasn’t
aware of.

Just did a quick test to see if I could get something working, the
code is attached.

Lets say you have ‘app/views/layouts/main.rhtml’ and
‘app/views/layouts/sub.rhtml’. In controllers that should have the sub
layout, you can set the layout explicitly to ‘sub’:


#6

John W. wrote:

Just did a quick test to see if I could get something working, the
code is attached.

Lots of good ideas to think about. I think I’ll eventually take up your
excercise for the reader, but probably not for this project (which is
due posthaste).

Thanks!

Jen