Edit on GitHub
Jump to docs navigation

Twig Components / Methods / html_classes

html_classes(args) is a Twig function to return 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>


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

Source: Twig

Edit this page on GitHub
Couldn't find what you were looking for? We are happy to help you in the forum, on Slack or on Github.