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?

On Mar 6, 6:47 pm, Jonzo [email protected] 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

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!

Thanks.

On Fri, Mar 7, 2008 at 4:39 PM, Robby R. [email protected]
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


Jonathan

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

Good luck!

Robby

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:

On Fri, Mar 7, 2008 at 5:31 PM, Robby R. [email protected]
wrote:

<%= yield :help %>

   "#{str} &raquo;"


Jonathan


Jonathan