This project is archived and is in readonly mode.
Add method to get to content captured with content_for from helper methods
Reported by Brian Durand | May 5th, 2010 @ 02:30 PM | in 2.3.6
In Rails 3 the only way to get for content captured with the content_for method in a view is to call yield from within a view. However, sometimes is it useful to get to this data from within a helper (for instance to provide a default value). Calling yield from within the helper method won't work, and accessing the @_content_for variable directly is not encouraged.
Proposed solution: add a captured_content_for method so helper
methods can legitimately access captured content. Add to
/action_pack/lib/action_view/helpers/capture_helper.rb
# Get content previously captured with +content_for+. This is the same as calling
# +yield+ inside a template except that this method can be used inside helper methods.
def captured_content_for(name)
@_content_for(name)
end
Comments and changes to this ticket
-
Jeremy Kemper May 5th, 2010 @ 03:04 PM
You can use
content_for(:foo)
yield
. I see now this isn't in the rdoc... -
Brian Durand May 5th, 2010 @ 03:18 PM
Great, 'cuz I was thinking that would be the perfect name for the method but it was already taken.
-
Brian Durand May 5th, 2010 @ 07:02 PM
Yep, understood. Just to confirm, will you add a comment to the rdoc?
-
Jeremy Kemper May 14th, 2010 @ 10:18 PM
- Milestone set to 2.3.6
- Tag changed from 3.0, actionview, helpers to 3.0, actionview, bugmash, helpers
Keep meaning to but my to-dos are a mile long. Tagging for Bugmash to update docs.
-
Jeroen van Dijk May 15th, 2010 @ 12:51 AM
- Tag changed from 3.0, actionview, bugmash, helpers to 3.0, actionview, bugmash, docs, helpers, patch
I have created a first version of documentation for this. I have access to the docrails project, but I would like to know if people think this is clear enough.
I'm also thinking that the whole yield syntax has no extra value anymore and should maybe be discouraged in documentation by not mentioning it.
If others can confirm that this is clear enough, I'll push it to docrails.
-
Jeff Kreeftmeijer May 15th, 2010 @ 01:30 PM
+1
Clear. I don't agree on removing the yield syntax though, the way you're doing it now is clearly discouraging the use of yield, which feels better than not documenting it at all. :)
-
Jeroen van Dijk May 15th, 2010 @ 02:38 PM
- Assigned user set to José Valim
Thanks for the confirmation Jeff. Documentation has been pushed to docrails: http://github.com/lifo/docrails/commit/f45714bb4a45097834dda2da03f8...
Ticket can be closed.
-
José Valim May 15th, 2010 @ 05:44 PM
- State changed from new to resolved
-
Ryan Bigg October 9th, 2010 @ 10:14 PM
- Tag cleared.
- Importance changed from to Low
Automatic cleanup of spam.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>