Skip to content

Latest commit

 

History

History
43 lines (27 loc) · 1.1 KB

html_classes.rst

File metadata and controls

43 lines (27 loc) · 1.1 KB

html_classes

The html_classes function returns a string by conditionally joining class names together:

<p class="{{ html_classes('a-class', 'another-class', {
    'errored': object.errored,
    'finished': object.finished,
    'pending': object.pending,
}) }}">How are you doing?</p>

For this example, knowing that object.errored and object.pending are true, this will render:

<p class="a-class another-class errored 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:

$ composer require twig/html-extra

Then, on Symfony projects, install the twig/extra-bundle:

$ 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());