replace content_for?

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?

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   def set_title(str="")     unless str.blank?       content_for :title do        "#{str} &raquo;"       end     end   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

yeah that does help :slight_smile: 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!


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


Good luck!


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! :stuck_out_tongue: