# Efficient use of layouts - how do I?

**URL:** <https://www.ruby-forum.com/t/efficient-use-of-layouts-how-do-i/49767>\
**Category:** Rails\
**Created:** [December 8, 2005, 4:34pm UTC](https://www.ruby-forum.com/t/efficient-use-of-layouts-how-do-i/49767 "2005-12-08T16:34:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [December 8, 2005, 4:34pm UTC](https://www.ruby-forum.com/t/efficient-use-of-layouts-how-do-i/49767/1 "2005-12-08T16:34:03Z")

</div>

Hi:

I have one application and four controllers. 3 controllers need  
identical code in their layouts. The fourth is a superset of the  
other 3. To be more specific, there is 10 lines of code buried  
between the & tags that only is required for the  
fourth controller.

What is the most efficient way to get this done? I know that if they  
all required the same code, that I could just put that code in a  
file called application.rhtml at the root layout level, but that is  
not quite the case here.

Thanks in advance for any suggestions.

bruce

PS. Would anyone also like to recommend to me an excellent book on  
object oriented programming? If it could be based on ruby that would  
be ideal but not necessary.

---

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [December 8, 2005, 5:39pm UTC](https://www.ruby-forum.com/t/efficient-use-of-layouts-how-do-i/49767/2 "2005-12-08T17:39:44Z")

</div>

On 12/8/05, Bruce B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> file called application.rhtml at the root layout level, but that is  
> not quite the case here.
> 
> Thanks in advance for any suggestions.
> 
> bruce
> 
> PS. Would anyone also like to recommend to me an excellent book on  
> object oriented programming? If it could be based on ruby that would  
> be ideal but not necessary.

Use one layout for all 4 controllers and put this into the head section:

... ... \<%= @content\_for\_head %\>

Then in the odd controller views that need extra content:

\<% content\_for ‘head’ do -%\>  
…  
…  
\<% end -%\>

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [December 8, 2005, 5:47pm UTC](https://www.ruby-forum.com/t/efficient-use-of-layouts-how-do-i/49767/3 "2005-12-08T17:47:58Z")

</div>

Rick:

Thanks so much. Very cool new feature I did not know about (new to  
me). Much appreciated, as is the speed of your reply.

bruce

---

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [December 8, 2005, 6:25pm UTC](https://www.ruby-forum.com/t/efficient-use-of-layouts-how-do-i/49767/4 "2005-12-08T18:25:32Z")

</div>

Rick O. \<technoweenie@…\> writes:

> \<% content\_for ‘head’ do -%\>  
> …  
> …  
> \<% end -%\>
> 
> –  
> rick  
> [http://techno-weenie.net](http://techno-weenie.net)

I have two layout-related questions I’d like to piggy-back on this one.  
First,  
what other @content\_for\_… helpers may be lurking out there and where  
can I  
look to find them all.

Second, can layouts be nested? I know you can have named layouts, then  
rails  
next looks for one matching the name of your controller, then finally  
for  
application.rhtml. What I’d like to know is this. Say for example I  
define the  
outer border of my page in my application.rhtml and have my  
@content\_for\_body  
inside of a table. Can I then create controller layout files that would  
define  
the upper nav section as a table row to be included in the outer  
application  
layout, then have the bulk of my table built by a named layout replacing  
the  
@content\_for\_body in my controller layout?

Hopefully I’ve explained this well, but my question really is can you  
nest  
layouts based on the default rails search path?
