Skip to content

Instantly share code, notes, and snippets.

@tow8ie
Created October 14, 2009 07:52
Show Gist options
  • Save tow8ie/209871 to your computer and use it in GitHub Desktop.
Save tow8ie/209871 to your computer and use it in GitHub Desktop.
Helper method to inject another helper‘s block into a partial
# A technique borrowed from http://www.igvita.com/2007/03/15/block-helpers-and-dry-views-in-rails/
# Only need this helper once, it will provide an interface to convert a block into a partial.
# 1. Capture is a Rails helper which will 'capture' the output of a block into a variable
# 2. Merge the 'body' variable into our options hash
# 3. Render the partial with the given options hash. Just like calling the partial directly.
#
def block_to_partial(partial_name, options = {}, &block)
options.merge!(:body => capture(&block))
concat(render(:partial => partial_name, :locals => options))
end
# Example usage:
#
# def rounded_box(type, options = {}, &block)
# block_to_partial('shared/rounded_box', options.merge(:type => type), &block)
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment