# replace content\_for?

**URL:** <https://discuss.rubyonrails.org/t/replace-content-for/25959>\
**Category:** rubyonrails-talk\
**Created:** [March 7, 2008, 2:47am UTC](https://discuss.rubyonrails.org/t/replace-content-for/25959 "2008-03-07T02:47:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan\_Fantham](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@Jonathan\_Fantham](https://discuss.rubyonrails.org/u/Jonathan_Fantham)\
**Post date:** [March 7, 2008, 2:47am UTC](https://discuss.rubyonrails.org/t/replace-content-for/25959/1 "2008-03-07T02:47:27Z")

</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:** ![Robby\_Russell2](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@Robby\_Russell2](https://discuss.rubyonrails.org/u/Robby_Russell2)\
**Post date:** [March 7, 2008, 3:39am UTC](https://discuss.rubyonrails.org/t/replace-content-for/25959/2 "2008-03-07T03:39:54Z")

</div>

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 \<title\> tag in the html header.

# app/helpers/application\_helper.rb &nbsp;&nbsp;def set\_title(str="") &nbsp;&nbsp;&nbsp;&nbsp;unless str.blank? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content\_for :title do &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"#{str} &raquo;" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

# app/views/layouts/application.html.erb

\<title\>\<%= (title = yield :title) ? title : "Default title text &raquo;" %\>PLANET ARGON\</title\>

# in any view... this will set the title

\<% set\_title 'Something different' -%\>

Hope this helps!

Cheers, Robby

---

<div class="post-metadata">

**Author:** ![Jonathan\_Fantham](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@Jonathan\_Fantham](https://discuss.rubyonrails.org/u/Jonathan_Fantham)\
**Post date:** [March 7, 2008, 3:52am UTC](https://discuss.rubyonrails.org/t/replace-content-for/25959/3 "2008-03-07T03:52:18Z")

</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.

---

<div class="post-metadata">

**Author:** ![Robby\_Russell2](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@Robby\_Russell2](https://discuss.rubyonrails.org/u/Robby_Russell2)\
**Post date:** [March 7, 2008, 4:31am UTC](https://discuss.rubyonrails.org/t/replace-content-for/25959/4 "2008-03-07T04:31:39Z")

</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:** ![Jonathan\_Fantham](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@Jonathan\_Fantham](https://discuss.rubyonrails.org/u/Jonathan_Fantham)\
**Post date:** [March 7, 2008, 5:06am UTC](https://discuss.rubyonrails.org/t/replace-content-for/25959/5 "2008-03-07T05:06:49Z")

</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! 😛
