# Sub-layouts?

**URL:** <https://www.ruby-forum.com/t/sub-layouts/94321>\
**Category:** Rails\
**Created:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/sub-layouts/94321 "2005-11-12T12:49:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jennyw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7feea3/32.png) [@jennyw](https://www.ruby-forum.com/u/jennyw)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/sub-layouts/94321/1 "2005-11-12T12:49:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jennyw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7feea3/32.png) [@jennyw](https://www.ruby-forum.com/u/jennyw)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/sub-layouts/94321/2 "2005-11-12T12:49:54Z")

</div>

render :partial =\> ‘sub\_layout’ will do what you are after.

Warmest regards,  
Nathan.

---

<div class="post-metadata">

**Author:** ![jennyw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7feea3/32.png) [@jennyw](https://www.ruby-forum.com/u/jennyw)\
**Post date:** [November 12, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/sub-layouts/94321/3 "2005-11-12T12:50:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jennyw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7feea3/32.png) [@jennyw](https://www.ruby-forum.com/u/jennyw)\
**Post date:** [November 12, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/sub-layouts/94321/4 "2005-11-12T12:50:06Z")

</div>

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](mailto: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](http://lists.rubyonrails.org/mailman/listinfo/rails)

* * *

Yahoo! Mail - PC Magazine Editors’ Choice 2005

> **[\[object](https://mail.yahoo.com/)**
>
> \[object

---

<div class="post-metadata">

**Author:** ![jennyw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7feea3/32.png) [@jennyw](https://www.ruby-forum.com/u/jennyw)\
**Post date:** [November 12, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/sub-layouts/94321/5 "2005-11-12T12:50:08Z")

</div>

On 11/11/05, jennyw [removed\_email\_address@domain.invalid](mailto: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’:

---

<div class="post-metadata">

**Author:** ![jennyw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7feea3/32.png) [@jennyw](https://www.ruby-forum.com/u/jennyw)\
**Post date:** [November 12, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/sub-layouts/94321/6 "2005-11-12T12:50:10Z")

</div>

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
