instance method
content_tag
Ruby on Rails 2.2.3
Since v2.2.3Signature
content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)
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 class="strong"><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 class="strong">Hello world!</div>
Parameters
-
namereq -
content_or_options_with_blockopt = nil -
optionsopt = nil -
escapeopt = true -
blockblock
Source
# File actionpack/lib/action_view/helpers/tag_helper.rb, line 68
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_tag = content_tag_string(name, capture(&block), options, escape)
if block_called_from_erb?(block)
concat(content_tag)
else
content_tag
end
else
content_tag_string(name, content_or_options_with_block, options, escape)
end
end
Defined in actionpack/lib/action_view/helpers/tag_helper.rb line 68
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionView::Helpers::TagHelper