# Dynamic content outside of \<%= yeild %\> in layout

**URL:** <https://www.ruby-forum.com/t/dynamic-content-outside-of-yeild-in-layout/72042>\
**Category:** Rails\
**Created:** [October 13, 2006, 9:07pm UTC](https://www.ruby-forum.com/t/dynamic-content-outside-of-yeild-in-layout/72042 "2006-10-13T21:07:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jwm](https://www.ruby-forum.com/u/jwm)\
**Post date:** [October 13, 2006, 9:07pm UTC](https://www.ruby-forum.com/t/dynamic-content-outside-of-yeild-in-layout/72042/1 "2006-10-13T21:07:09Z")

</div>

Basicly, I have a standard layout with a main content section (the \<%=  
yeild %\>), but I also have a sidebar. Previously I was just using \<%  
render :partial =\> ‘path/to/partial’ %\>. Now I want the content in the  
sidebar to be dynamic, should I be using helpers, or can I call another  
controller and method from inside the rhtml?

thanks

---

<div class="post-metadata">

**Author:** ![jwm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jwm](https://www.ruby-forum.com/u/jwm)\
**Post date:** [October 13, 2006, 9:24pm UTC](https://www.ruby-forum.com/t/dynamic-content-outside-of-yeild-in-layout/72042/2 "2006-10-13T21:24:45Z")

</div>

The way that I do this is with content\_for. You can use  
content\_for\_layout in your main layouts to display individual view  
content (this is an alternate to yield - I think yield is  
deprecated?). In the same way you can put content\_for\_XXX in your  
layout and have that replaced as well. An example might be the  
easiest:

In your layout:

\< if !@content\_for\_sidebar.blank? %\>  
\<%= @content\_for\_sidebar %\>  
\<% end %\>

In your individual views:

\<% content\_for ‘sidebar’ do -%\>

Put some content here!

\<% end %\>

Note that you don’t need the if statement in the layout if you always  
fill out the content\_for\_sidebar but in my views I often leave some  
content sections empty.

Jon

On 10/13/06, Jonathon M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Jon W.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jwm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jwm](https://www.ruby-forum.com/u/jwm)\
**Post date:** [October 13, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/dynamic-content-outside-of-yeild-in-layout/72042/3 "2006-10-13T22:01:03Z")

</div>

On 10/13/06, Jon W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<%= @content\_for\_sidebar %\>  
> \<% end %\>

It is better to use \<%= yield ‘sidebar’ %\> instead.of \<%=  
@content\_for\_sidebar %\>.

In your individual views:

> \<% content\_for ‘sidebar’ do -%\>
> 
> Put some content here!
> 
> \<% end %\>
> 
> Note that you don’t need the if statement in the layout if you always  
> fill out the content\_for\_sidebar but in my views I often leave some  
> content sections empty.

If you just need to prepare data for your sidebar, you can use  
combination  
of common partial + before\_filter in e.g. ApplicationController to  
prepare  
data for partial.

But if your sidebar is dependant on particular section, I would suggest  
using nested\_layouts plugin ([http://nested-layouts.rubyforge.org](http://nested-layouts.rubyforge.org)) and  
“content\_for” helper (described above). So for every “section” (a group  
of  
pages with the same sidebar) you could use the same inner layout that  
would  
prepare sidebar content and wrap page content in outer (your main)  
layout.  
Take a look at nested\_layouts plugin documentation - it has example of  
just  
what (I guess) you need.

---

<div class="post-metadata">

**Author:** ![jwm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jwm](https://www.ruby-forum.com/u/jwm)\
**Post date:** [October 13, 2006, 10:36pm UTC](https://www.ruby-forum.com/t/dynamic-content-outside-of-yeild-in-layout/72042/4 "2006-10-13T22:36:43Z")

</div>

Jon, I thought content\_for was depricated.

Maxim, I think the before\_filter and common partial is what Im looking  
for. I can use the before\_filter to load up relavent data and use some  
basic logic in the rhtml to flip different sidebar ‘modules’ on and off.

I had tried render\_component, but heard that was a wasteful option, not  
to mention it crashed WEBrick on more than one occasion.

---

<div class="post-metadata">

**Author:** ![jwm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jwm](https://www.ruby-forum.com/u/jwm)\
**Post date:** [October 14, 2006, 10:07am UTC](https://www.ruby-forum.com/t/dynamic-content-outside-of-yeild-in-layout/72042/5 "2006-10-14T10:07:28Z")

</div>

> I thought content\_for was depricated.

Not exactly. Accessing @content\_for\_(…) is deprecated (see Jon W.'s  
post), but the content\_for() method itself is not deprecated. Or at  
least that’s how I read the docs.

---

<div class="post-metadata">

**Author:** ![jwm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jwm](https://www.ruby-forum.com/u/jwm)\
**Post date:** [October 13, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/dynamic-content-outside-of-yeild-in-layout/72042/6 "2006-10-13T23:00:05Z")

</div>

Jonathon,  
You are right. From the rails docs:

NOTE: Beware that content\_for is ignored in caches. So you shouldn’t  
use it for elements that are going to be fragment cached.

The deprecated way of accessing a content\_for block was to use a  
instance variable named @@content\_for\_#{name\_of\_the\_content\_block}@.  
So \<%= content\_for(‘footer’) %\> would be avaiable as \<%=  
@content\_for\_footer %\>. The preferred notation now is \<%= yield  
:footer %\>.

It is always good when I learn something by answering a question!

Jon
