Skip to content

Instantly share code, notes, and snippets.

@wulftone
Created October 27, 2011 21:30
Show Gist options
  • Save wulftone/1320939 to your computer and use it in GitHub Desktop.
Save wulftone/1320939 to your computer and use it in GitHub Desktop.
Make your own template block evaluator thing
# 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