Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Add method to get to content captured with content_for from helper methods

#4537

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

Reported by Brian Durand · May 5th, 2010 @ 02:30 PM

State: resolved
Milestone: 2.3.6
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. Jeremy Kemper
    Jeremy Kemper

    You can use

    content_for(:foo)
    
    in place of yield. I see now this isn't in the rdoc...

    May 5th, 2010 @ 03:04 PM

  2. Brian Durand
    Brian Durand

    Great, 'cuz I was thinking that would be the perfect name for the method but it was already taken.

    May 5th, 2010 @ 03:18 PM

  3. Jeremy Kemper
    Jeremy Kemper

    I mean it already works ;-)

    May 5th, 2010 @ 06:02 PM

  4. Brian Durand
    Brian Durand

    Yep, understood. Just to confirm, will you add a comment to the rdoc?

    May 5th, 2010 @ 07:02 PM

  5. Jeremy Kemper
    Jeremy Kemper
    • 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.

    May 14th, 2010 @ 10:18 PM

  6. Jeroen van Dijk
    Jeroen van Dijk
    • 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.

    May 15th, 2010 @ 12:51 AM

  7. Jeff Kreeftmeijer
    Jeff Kreeftmeijer

    +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. :)

    May 15th, 2010 @ 01:30 PM

  8. Jeroen van Dijk
    Jeroen van Dijk
    • 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.

    May 15th, 2010 @ 02:38 PM

  9. José Valim
    José Valim
    • State changed from new to resolved

    May 15th, 2010 @ 05:44 PM

  10. Ryan Bigg
    Ryan Bigg
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    October 9th, 2010 @ 10:14 PM

  11. bingbing