Created
October 14, 2009 07:52
-
-
Save tow8ie/209871 to your computer and use it in GitHub Desktop.
Helper method to inject another helper‘s block into a partial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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