Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

content_tag that accepts a block = block_tag (See related posts)

Sometimes I want to do something like this:

<% content_tag("div", :class => "section_with_error" do %>
  HTML GOES HERE
<% end %>


instead of this:

<%= content_tag("div", "HTML GOES HERE", :class => "section_with_error") %>


Put the following code in ApplicationHelper, and you can do just that (replacing "content_tag" in the example above with "block_tag", of course):

  def block_tag(tag, options = {}, &block)
    concat(content_tag(tag, capture(&block), options), block.binding)
  end

You need to create an account or log in to post comments to this site.


Click here to browse all 4857 code snippets

Related Posts