# Replace content\_for?

**URL:** <https://www.ruby-forum.com/t/replace-content-for/131233>\
**Category:** Rails\
**Created:** [March 7, 2008, 3:48am UTC](https://www.ruby-forum.com/t/replace-content-for/131233 "2008-03-07T03:48:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfaulkenberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@jfaulkenberry](https://www.ruby-forum.com/u/jfaulkenberry)\
**Post date:** [March 7, 2008, 3:48am UTC](https://www.ruby-forum.com/t/replace-content-for/131233/1 "2008-03-07T03:48:21Z")

</div>

I’ve been using content\_for a lot to put things in the header like  
extra stylesheets or javascript includes, which is great because it  
just appends it all and it all works nicely.

I have a problem where I want to declare a content space like when I  
use content for:

\<%= yield :help %\>

But I want to make any content\_for declarations override what was  
previously in there.

Specifically I’m using it for help (you might have guessed :-P). I  
have a global generic help message, replaced in layouts with a more  
specific message, which in turn could be replaced by page specific  
help if necessary.

what’s the best way of doing this?

---

<div class="post-metadata">

**Author:** ![jfaulkenberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@jfaulkenberry](https://www.ruby-forum.com/u/jfaulkenberry)\
**Post date:** [March 7, 2008, 4:40am UTC](https://www.ruby-forum.com/t/replace-content-for/131233/2 "2008-03-07T04:40:36Z")

</div>

On Mar 6, 6:47 pm, Jonzo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> previously in there.
> 
> Specifically I’m using it for help (you might have guessed :-P). I  
> have a global generic help message, replaced in layouts with a more  
> specific message, which in turn could be replaced by page specific  
> help if necessary.
> 
> what’s the best way of doing this?

If I’m reading this right… you’re essentially looking for a way to  
set a default value for the yield and then have the ability to write  
over it?

We do this for things like the tag in the html header.

# app/helpers/application\_helper.rb

def set\_title(str=“”)  
unless str.blank?  
content\_for :title do  
“#{str} »”  
end  
end  
end

# app/views/layouts/application.html.erb
\<%= (title = yield :title) ? title : "Default title text »" %\>PLANET ARGON
# in any view… this will set the title

\<% set\_title ‘Something different’ -%\>

Hope this helps!

Cheers,  
Robby

–  
Robby R.  
[http://www.robbyonrails.com](http://www.robbyonrails.com)

---

<div class="post-metadata">

**Author:** ![jfaulkenberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@jfaulkenberry](https://www.ruby-forum.com/u/jfaulkenberry)\
**Post date:** [March 7, 2008, 4:52am UTC](https://www.ruby-forum.com/t/replace-content-for/131233/3 "2008-03-07T04:52:54Z")

</div>

yeah that does help 🙂 I’m guessing that there’s no standard way of  
doing this then, but you’re solution looks nice. You could probably  
even put blocks in there if you were really keen!

Thanks.

On Fri, Mar 7, 2008 at 4:39 PM, Robby R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > what’s the best way of doing this?  
> > content\_for :title do
> 
> # in any view… this will set the title
> 
> [http://www.robbyonrails.com](http://www.robbyonrails.com)
> 
> >

–  
Jonathan

---

<div class="post-metadata">

**Author:** ![jfaulkenberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@jfaulkenberry](https://www.ruby-forum.com/u/jfaulkenberry)\
**Post date:** [March 7, 2008, 5:32am UTC](https://www.ruby-forum.com/t/replace-content-for/131233/4 "2008-03-07T05:32:08Z")

</div>

Yep. One of our developers blogged about that last year.

- [http://blog.imperialdune.com/2007/3/27/dirty-views-clean-them-up](http://blog.imperialdune.com/2007/3/27/dirty-views-clean-them-up)

Good luck!

Robby

---

<div class="post-metadata">

**Author:** ![jfaulkenberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c2a13f/32.png) [@jfaulkenberry](https://www.ruby-forum.com/u/jfaulkenberry)\
**Post date:** [March 7, 2008, 6:07am UTC](https://www.ruby-forum.com/t/replace-content-for/131233/5 "2008-03-07T06:07:18Z")

</div>

hey that’s pretty cool, did he ever consider making it into a plugin?  
It’d be small and simple but I’d use it! especially if he added in  
overwritable\_content\_for! 😛

On Fri, Mar 7, 2008 at 5:31 PM, Robby R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > > \<%= yield :help %\>
> > 
> > > ```
> > > "#{str} &raquo;"
> > > 
> > > ```
> > 
> > –  
> > Jonathan
> 
> >

–  
Jonathan
