Returns an HTML block tag of type name surrounding the content. Add HTML attributes by passing an attributes hash to options. Instead of passing the content as an argument, you can also use a block in which case, you pass your options as the second parameter. Set escape to false to disable attribute value escaping.
Options
The options hash is used with attributes with no value like (disabled and readonly), which you can give a value of true in the options hash. You can use symbols or strings for the attribute names.
Examples
content_tag(:p, "Hello world!") # => <p>Hello world!</p> content_tag(:div, content_tag(:p, "Hello world!"), :class => "strong") # => <div><p>Hello world!</p></div> content_tag("select", options, :multiple => true) # => <select multiple="multiple">...options...</select> <% content_tag :div, :class => "strong" do -%> Hello world! <% end -%> # => <div><p>Hello world!</p></div>
Source Code
# File action_view/helpers/tag_helper.rb, line 66 def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block) if block_given? options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash) content = capture(&block) content_tag = content_tag_string(name, content, options, escape) block_is_within_action_view?(block) ? concat(content_tag, block.binding) : content_tag else content = content_or_options_with_block content_tag_string(name, content, options, escape) end end
<code/>and<pre/>for code samples.