Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 1 | ``html_classes`` |
| 2 | ================ |
| 3 | |
| 4 | The ``html_classes`` function returns a string by conditionally joining class |
| 5 | names together: |
| 6 | |
| 7 | .. code-block:: html+twig |
| 8 | |
| 9 | <p class="{{ html_classes('a-class', 'another-class', { |
| 10 | 'errored': object.errored, |
| 11 | 'finished': object.finished, |
| 12 | 'pending': object.pending, |
| 13 | }) }}">How are you doing?</p> |
| 14 | |
| 15 | .. note:: |
| 16 | |
| 17 | The ``html_classes`` function is part of the ``HtmlExtension`` which is not |
| 18 | installed by default. Install it first: |
| 19 | |
| 20 | .. code-block:: bash |
| 21 | |
| 22 | $ composer require twig/html-extra |
| 23 | |
| 24 | Then, on Symfony projects, install the ``twig/extra-bundle``: |
| 25 | |
| 26 | .. code-block:: bash |
| 27 | |
| 28 | $ composer require twig/extra-bundle |
| 29 | |
| 30 | Otherwise, add the extension explicitly on the Twig environment:: |
| 31 | |
| 32 | use Twig\Extra\Html\HtmlExtension; |
| 33 | |
| 34 | $twig = new \Twig\Environment(...); |
| 35 | $twig->addExtension(new HtmlExtension()); |