Twig

The flexible, fast, and secure
template engine for PHP

a Symfony Product
Docs Functions html_classes
You are reading the documentation for Twig 3.x. Switch to the documentation for Twig 1.x, 2.x.

Questions & Feedback

License

Twig documentation is licensed under the new BSD license.

html_classes

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 require twig/html-extra

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

1
$ composer require twig/extra-bundle

Otherwise, add the extension explicitly on the Twig environment:

1
2
3
4
use Twig\Extra\Html\HtmlExtension;

$twig = new \Twig\Environment(...);
$twig->addExtension(new HtmlExtension());