# One layout - many templates

**URL:** <https://www.ruby-forum.com/t/one-layout-many-templates/170628>\
**Category:** Rails\
**Created:** [June 19, 2009, 8:51pm UTC](https://www.ruby-forum.com/t/one-layout-many-templates/170628 "2009-06-19T20:51:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lecielbleu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b487fb/32.png) [@lecielbleu](https://www.ruby-forum.com/u/lecielbleu)\
**Post date:** [June 19, 2009, 8:51pm UTC](https://www.ruby-forum.com/t/one-layout-many-templates/170628/1 "2009-06-19T20:51:01Z")

</div>

Hi , imagine that you have one layout and every point of that is made  
with another templates. how can i handle this situation. For example  
in the left column ,displaying list of subcategories depending on  
category which is displayed at this time in the center of site (\<%=  
yield :layout %\> line). it might be develop , making navigations  
randomly changing like [amazon.com](http://amazon.com)

---

<div class="post-metadata">

**Author:** ![lecielbleu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b487fb/32.png) [@lecielbleu](https://www.ruby-forum.com/u/lecielbleu)\
**Post date:** [June 19, 2009, 8:54pm UTC](https://www.ruby-forum.com/t/one-layout-many-templates/170628/2 "2009-06-19T20:54:34Z")

</div>

> Hi , imagine that you have one layout and every point of that is made  
> with another templates. how can i handle this situation. For example  
> in the left column ,displaying list of subcategories depending on  
> category which is displayed at this time in the center of site (\<%=  
> yield :layout %\> line). it might be develop , making navigations  
> randomly changing like [amazon.com](http://amazon.com)

I typically do it like this:

layouts/application.html.erb:

…

 \<%= yield %\>
...
 \<%= yield :left\_column %\>

Then in my other views I do whatever is simplest to generate content  
for the left column… perhaps…

\<% content\_for :left\_column do %\>  
\<%= render :partial =\> ‘left\_column’ %\>  
\<% end %\>

Where that would pick up a specific left\_column partial for that  
controller. That sort of thing…

-philip

---

<div class="post-metadata">

**Author:** ![lecielbleu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b487fb/32.png) [@lecielbleu](https://www.ruby-forum.com/u/lecielbleu)\
**Post date:** [June 19, 2009, 10:09pm UTC](https://www.ruby-forum.com/t/one-layout-many-templates/170628/3 "2009-06-19T22:09:10Z")

</div>

\<% content\_for :left\_column do %\>  
\<%= render :partial =\> ‘left\_column’ %\>  
\<% end %\>

where this codes should be? in the layout folder?

if i understand , i must generate 2 files (one is template ,other one  
is \_left\_column.html.erb partial templats) , am i right?

---

<div class="post-metadata">

**Author:** ![lecielbleu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b487fb/32.png) [@lecielbleu](https://www.ruby-forum.com/u/lecielbleu)\
**Post date:** [June 20, 2009, 11:02am UTC](https://www.ruby-forum.com/t/one-layout-many-templates/170628/4 "2009-06-20T11:02:35Z")

</div>

what will be if i display another controller , it will give me  
undefined method cause left column is depended on product  
controller ,i want a template working in many controllers

---

<div class="post-metadata">

**Author:** ![lecielbleu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b487fb/32.png) [@lecielbleu](https://www.ruby-forum.com/u/lecielbleu)\
**Post date:** [June 20, 2009, 4:43pm UTC](https://www.ruby-forum.com/t/one-layout-many-templates/170628/5 "2009-06-20T16:43:33Z")

</div>

is there any sample code for this navigation, i tried to put variable  
in application controller , but it didnt work ,  
when i put @var =“abc” in application controller then i wrote \<%=  
@var %\> , it displayed nothing.

---

<div class="post-metadata">

**Author:** ![lecielbleu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b487fb/32.png) [@lecielbleu](https://www.ruby-forum.com/u/lecielbleu)\
**Post date:** [June 19, 2009, 10:14pm UTC](https://www.ruby-forum.com/t/one-layout-many-templates/170628/6 "2009-06-19T22:14:28Z")

</div>

On Jun 19, 2009, at 1:08 PM, lecielbleu wrote:

> \<% content\_for :left\_column do %\>  
> \<%= render :partial =\> ‘left\_column’ %\>  
> \<% end %\>
> 
> where this codes should be? in the layout folder?

No… that would go in say /app/views/products/\_left\_column.html.erb

THen when you’re in your products views you could use the above to  
load up that left column.
