html_classes

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

html_classes 関数は、クラス名を条件付きで結合して文字列を返します。
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:

html_classes 関数は、デフォルトではインストールされない HtmlExtension の一部です。最初にインストールします。
1
$ composer require twig/html-extra

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

次に、Symfony プロジェクトで、twig/extra-bundle をインストールします。
1
$ composer require twig/extra-bundle

Otherwise, add the extension explicitly on the Twig environment:

それ以外の場合は、Twig 環境で拡張機能を明示的に追加します。
1
2
3
4
use Twig\Extra\Html\HtmlExtension;

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