html_classes
ΒΆ
New in version 2.12: The html_classes
function was added in Twig 2.12.
The html_classes
function returns a string by conditionally joining class
names together:
1 2 3 4 5 | <p class="{{ html_classes('a-class', 'another-class', {
'errored': object.errored,
'finished': object.finished,
'pending': object.pending,
}) }}">How are you doing?</p>
|
Note
The html_classes
function is part of the HtmlExtension
which is not
installed by default. Install it first:
1 | $ composer req twig/html-extra
|
Then, use the twig/extra-bundle
on Symfony projects or add the extension
explictly on the Twig environment:
use Twig\Extra\Html\HtmlExtension;
$twig = new \Twig\Environment(...);
$twig->addExtension(new HtmlExtension());