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.
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.
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.
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’:
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.