tag(name = nil, options = nil, open = false, escape = true) public

Returns an HTML tag.

Building HTML tags

Builds HTML5 compliant tags with a tag proxy. Every tag can be built with:

tag.<tag name>(optional content, options)

where tag name can be e.g. br, div, section, article, or any tag really.

Passing content

Tags can pass content to embed within it:

tag.h1 'All titles fit to print' # => <h1>All titles fit to print</h1>

tag.div tag.p('Hello world!')  # => <div><p>Hello world!</p></div>

Content can also be captured with a block, which is useful in templates:

<%= tag.p do %>
  The next great American novel starts here.
<% end %>
# => <p>The next great American novel starts here.</p>


Use symbol keyed options to add attributes to the generated tag.

tag.section class: %w( kitties puppies )
# => <section class="kitties puppies"></section>

tag.section id: dom_id(@post)
# => <section id="<generated dom id>"></section>

Pass true for any attributes that can render with no values, like disabled and readonly.

tag.input type: 'text', disabled: true
# => <input type="text" disabled="disabled">

HTML5 data-* and aria-* attributes can be set with a single data or aria key pointing to a hash of sub-attributes.

To play nicely with JavaScript conventions, sub-attributes are dasherized.

tag.article data: { user_id: 123 }
# => <article data-user-id="123"></article>

Thus data-user-id can be accessed as dataset.userId.

Data attribute values are encoded to JSON, with the exception of strings, symbols and BigDecimals. This may come in handy when using jQuery’s HTML5-aware .data() from 1.4.3.

tag.div data: { city_state: %w( Chicago IL ) }
# => <div data-city-state="["Chicago","IL"]"></div>

The generated tag names and attributes are escaped by default. This can be disabled using escape.

tag.img src: 'open & shut.png'
# => <img src="open & shut.png">

tag.img src: 'open & shut.png', escape: false
# => <img src="open & shut.png">

The tag builder respects HTML5 void elements if no content is passed, and omits closing tags for those elements.

# A standard element:
tag.div # => <div></div>

# A void element:
tag.br  # => <br>

Legacy syntax

The following format is for legacy syntax support. It will be deprecated in future versions of Rails.

tag(name, options = nil, open = false, escape = true)

It returns an empty HTML tag of type name which by default is XHTML compliant. Set open to true to create an open tag compatible with HTML 4.0 and below. Add HTML attributes by passing an attributes hash to options. Set escape to false to disable attribute value escaping.


You can use symbols or strings for the attribute names.

Use true with boolean attributes that can render with no value, like disabled and readonly.

HTML5 data-* attributes can be set with a single data key pointing to a hash of sub-attributes.


# => <br />

tag("br", nil, true)
# => <br>

tag("input", type: 'text', disabled: true)
# => <input type="text" disabled="disabled" />

tag("input", type: 'text', class: ["strong", "highlight"])
# => <input class="strong highlight" type="text" />

tag("img", src: "open & shut.png")
# => <img src="open & shut.png" />

tag("img", { src: "open & shut.png" }, false, false)
# => <img src="open & shut.png" />

tag("div", data: { name: 'Stephen', city_state: %w(Chicago IL) })
# => <div data-name="Stephen" data-city-state="["Chicago","IL"]" />

tag("div", class: { highlight: current_user.admin? })
# => <div class="highlight" />
Show source
Register or log in to add new notes.
April 19, 2012 - (v3.1.0 - v3.2.1)
0 thanks

Missed close tag

At the page http://apidock.com/rails/ActionView/Helpers/TagHelper/tag

<tt>.data()</tt> should be instead of <tt>.data()<tt>