``html_attr``
=============
.. _html_attr:
The ``html_attr`` function renders HTML attributes from one or more mappings,
taking care of proper escaping. The mappings contain the names of HTML
attributes as keys, and the corresponding values represent the attributes'
values.
.. note::
Attribute names are escaped using the ``html_attr_relaxed`` strategy.
.. code-block:: html+twig
Content
{# Output:
Content
#}
The function accepts multiple attribute maps. Internally, it uses
:ref:`html_attr_merge` to combine the arguments:
.. code-block:: html+twig
{% set base_attrs = {class: ['btn']} %}
{% set variant_attrs = {class: ['btn-primary'], disabled: true} %}
{# Output: #}
.. note::
To make best use of the special merge behavior of ``html_attr_merge`` and
to avoid confusion, you should consistently use iterables (mappings or sequences)
for attributes that can take multiple values, like ``class``, ``srcset`` or ``aria-describedby``.
Use non-iterable values for attributes that contain a single value only, like
``id`` or ``href``.
Shorthand notation for mappings can be particularly helpful:
.. code-block:: html+twig
{% set id = 'user-123' %}
{% set href = '/profile' %}
Profile
{# Output: Profile #}
``null`` and Boolean Attribute Values
-------------------------------------
``null`` values always omit printing an attribute altogether.
The boolean ``false`` value also omits the attribute altogether, with an
exception for ``aria-*`` attribute names, see below.
.. code-block:: html+twig
{# null omits the attribute entirely, and so does false for non-"aria-*" #}
{# Output: #}
``true`` will print the attribute with the empty value ``""``. This is XHTML compatible,
and in HTML 5 equivalent to using the short attribute notation without a value. An exception
is made for ``data-*`` and ``aria-*`` attributes, see below.
.. code-block:: html+twig
{# true becomes an empty string value #}
{# Output: , which is equivalent to #}
Array Values
------------
Attribute values that are iterables are automatically converted to space-separated
token lists of the values. Exceptions apply for ``data-*`` and ``style`` attributes,
described further below.
.. code-block:: html+twig
Button
{# Output:
Button
#}
.. note::
This is not bound to the ``class`` attribute name, but works for any attribute.
You can use the :ref:`html_attr_type` filter to specify a different strategy for
concatenating values (e.g., comma-separated for ``srcset`` attributes). This would
also override the special behavior for ``data-*`` and ``style``.
WAI-ARIA Attributes
-------------------
To make it more convenient to work with the `WAI-ARIA type mapping for HTML
_`, boolean values for ``aria-*``
attributes are converted to strings ``"true"`` and ``"false"``.
.. code-block:: html+twig
{# Output: #}
Data Attributes
---------------
For ``data-*`` attributes, a boolean ``true`` is converted to the string
``"true"``, and any non-scalar value is JSON-encoded, including arrays and
other iterables. The only exception is a ``Stringable`` object, which behaves
as it does for any other attribute: it is cast to its string representation,
or rendered as a token list when it is also iterable.
.. code-block:: html+twig
Content
{# Output:
Content
#}
Style Attribute
----------------
The ``style`` attribute name has special handling when its value is iterable:
.. code-block:: html+twig
{# Non-numeric keys will be used as CSS properties and printed #}
Styled text
{# Output:
Styled text
#}
{# Numeric keys will be assumed to have values that are individual CSS declarations #}
Styled text
{# Output:
Styled text
#}
{# Merging style attributes #}
Styled text
{# Output:
Styled text
#}
.. warning::
No additional escaping specific to CSS is applied to key or values from this array.
Do not use it to pass untrusted, user-provided data, neither as key nor as value.
``AttributeValueInterface`` Implementations
-------------------------------------------
For advanced use cases, attribute values can be objects that implement the ``AttributeValueInterface``.
These objects can define their own conversion logic for the ``html_attr`` function that will take
precedence over all rules described here. See the docblocks in that interface for details.
.. note::
The ``html_attr`` function is part of the ``HtmlExtension`` which is not
installed by default. Install it first:
.. code-block:: bash
$ composer require twig/html-extra
Then, on Symfony projects, install the ``twig/extra-bundle``:
.. code-block:: bash
$ composer require twig/extra-bundle
Otherwise, add the extension explicitly on the Twig environment::
use Twig\Extra\Html\HtmlExtension;
$twig = new \Twig\Environment(...);
$twig->addExtension(new HtmlExtension());
.. seealso::
:ref:`html_attr_merge`,
:ref:`html_attr_type`