Created
October 27, 2011 21:30
-
-
Save wulftone/1320939 to your computer and use it in GitHub Desktop.
Make your own template block evaluator thing
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
# In a helper | |
def box(&block) | |
("<div class='box'>" + capture(&block) + "</div>").html_safe | |
end | |
# In your view template | |
#<%= box do %> | |
# <p>Hello World!</p> | |
#<% end %> | |
# Same thing, but with some options to specify CSS class | |
def box(options = {}, &block) | |
options = {:class => ''}.merge(options) # Make sure the key exists, since we're using it later | |
("<div class='#{options[:class]}'>" + capture(&block) + "</div>").html_safe | |
end | |
# Alternative method of doing the same thing | |
def box(options = {}, &block) | |
("<div class='#{options.has_key?(:class) ? options[:class] : ''}'>" + capture(&block) + "</div>").html_safe | |
end | |
# In your view template | |
#<%= box :class => 'some-string' do %> | |
# <p>Hello World!</p> | |
#<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment