Feature: #110769 - Allow boolean and arbitrary values in HTML attributes
See forge#110769
Description
\TYPO3\ now has an
optional fourth parameter
$convert. When enabled, values are
converted for HTML5 output:
truerenders an attribute without a value, for exampledeferinstead ofdefer="defer"falseandnullomit the attribute- objects implementing
\Stringableare cast to string - other arrays and objects are JSON-encoded and escaped for the attribute
- all other values are escaped with
htmlspecialchars()
Example:
GeneralUtility::implodeAttributes([
'src' => '/file.js',
'defer' => true,
'nomodule' => false,
'data-config' => ['a' => 1],
], true, false, true);
// src="/file.js" defer data-config="{"a":1}"
The option is used when rendering script, link and style tags in
\TYPO3\,
\TYPO3\ and
\TYPO3\, as well as for the
<meta>
tags of the meta tag managers, the
<html>
tag
configured via
config. and the
<video>
and
<audio>
tags.
Impact
Extension authors can pass boolean values and arrays as tag attributes, for
example via tag of the
Page or the
additional option of the video and audio renderers.
The default behavior of
implode is unchanged.
In the video and audio renderers,
true
in additional
now renders an attribute without a value (
playsinline
) instead of
playsinline="1"
, and
false
removes the attribute, including
default ones such as
controls
.
Enumerated attributes such as
aria- or
draggable
must
still be passed as the strings
'true'
or
'false'
, since
omitting them is not the same as setting them to "false". Attributes without a
value are not XML-compliant.