module ActionView::Helpers::TagHelper
def tag(name = nil, options = nil, open = false, escape = true)
tag("div", class: { highlight: current_user.admin? })
# =>
tag("div", data: { name: 'Stephen', city_state: %w(Chicago IL) })
# =>
tag("img", { src: "open & shut.png" }, false, false)
# =>
tag("img", src: "open & shut.png")
# =>
tag("input", type: 'text', class: ["strong", "highlight"])
# =>
tag("input", type: 'text', disabled: true)
# =>
tag("br", nil, true)
# =>
tag("br")
==== Examples
pointing to a hash of sub-attributes.
HTML5 data-* attributes can be set with a single +data+ key
+disabled+ and +readonly+.
Use +true+ with boolean attributes that can render with no value, like
You can use symbols or strings for the attribute names.
==== Options
escaping.
hash to +options+. Set +escape+ to false to disable attribute value
with HTML 4.0 and below. Add HTML attributes by passing an attributes
compliant. Set +open+ to true to create an open tag compatible
It returns an empty HTML tag of type +name+ which by default is XHTML
tag(name, options = nil, open = false, escape = true)
The following format is for legacy syntax support. It will be deprecated in future versions of \Rails.
=== Legacy syntax
# =>
# =>
>
prefixed attributes:
aria: or data: objects into aria- and data-
Transforms keys nested within
ERB. Includes or omits boolean attributes based on their truthiness.
Transforms a Hash into HTML attributes, ready to be interpolated into
=== Building HTML attributes
tag.br # =>
# A void element:
tag.div # =>
# A standard element:
if no content is passed, and omits closing tags for those elements.
{HTML5 void elements}[https://www.w3.org/TR/html5/syntax.html#void-elements]
The tag builder respects
# =>
tag.img src: 'open & shut.png', escape: false
# =>
tag.img src: 'open & shut.png'
+escape+.
The generated tag names and attributes are escaped by default. This can be disabled using
# =>
tag.div data: { city_state: %w( Chicago IL ) }
from 1.4.3.
This may come in handy when using jQuery's HTML5-aware .data()
BigDecimals.
Data attribute values are encoded to JSON, with the exception of strings, symbols, and
Thus data-user-id can be accessed as dataset.userId.
# =>
tag.article data: { user_id: 123 }
To play nicely with JavaScript conventions, sub-attributes are dasherized.
single +data+ or +aria+ key pointing to a hash of sub-attributes.
HTML5 data-* and aria-* attributes can be set with a
# =>
tag.input type: 'text', disabled: true
Pass +true+ for any attributes that can render with no values, like +disabled+ and +readonly+.
# =>
tag.section id: dom_id(@post)
# =>
tag.section class: %w( kitties puppies )
Use symbol keyed options to add attributes to the generated tag.
==== Options
# =>
The next great American novel starts here.
<% end %>
The next great American novel starts here.
<%= tag.p do %>
Content can also be captured with a block, which is useful in templates:
tag.div tag.p('Hello world!') # =>
Hello world!
tag.h1 'All titles fit to print' # =>
All titles fit to print
Tags can pass content to embed within it:
==== Passing content
where tag name can be e.g. br, div, section, article, or any tag really.
tag.
Builds HTML5 compliant tags with a tag proxy. Every tag can be built with:
=== Building HTML tags
Returns an HTML tag.
def tag(name = nil, options = nil, open = false, escape = true) if name.nil? tag_builder else name = ERB::Util.xml_name_escape(name) if escape "<#{name}#{tag_builder.tag_options(options, escape) if options}#{open ? ">" : " />"}".html_safe end end