Twig

The flexible, fast, and secure
template engine for PHP

a Symfony Product
Docs Functions html_cva
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_cva

3.12

The html_cva function was added in Twig 3.12.

CVA (Class Variant Authority) is a concept from the JavaScript world and used by the well-known shadcn/ui library. The CVA concept is used to render multiple variations of components, applying a set of conditions and recipes to dynamically compose CSS class strings (color, size, etc.), to create highly reusable and customizable templates.

The concept of CVA is powered by a html_cva() Twig function where you define base classes that should always be present and then different variants and the corresponding classes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{# templates/alert.html.twig #}
{% set alert = html_cva(
    base='alert ',
    variants={
        color: {
            blue: 'bg-blue',
            red: 'bg-red',
            green: 'bg-green',
        },
        size: {
            sm: 'text-sm',
            md: 'text-md',
            lg: 'text-lg',
        }
    }
) %}

<div class="{{ alert.apply({color, size}, class) }}">
    ...
</div>

Then use the color and size variants to select the needed classes:

1
2
3
4
5
6
7
8
9
{# index.html.twig #}
{{ include('alert.html.twig', {'color': 'blue', 'size': 'md'}) }}
// class="alert bg-red text-lg"

{{ include('alert.html.twig', {'color': 'green', 'size': 'sm'}) }}
// class="alert bg-green text-sm"

{{ include('alert.html.twig', {'color': 'red', 'class': 'flex items-center justify-center'}) }}
// class="alert bg-red text-md flex items-center justify-center"

CVA and Tailwind CSS

CVA work perfectly with Tailwind CSS. The only drawback is that you can have class conflicts. To "merge" conflicting classes together and keep only the ones you need, use the tailwind_merge() filter from tales-from-a-dev/twig-tailwind-extra with the html_cva() function:

1
$ composer require tales-from-a-dev/twig-tailwind-extra
1
2
3
4
5
6
7
{% set alert = html_cva(
   // ...
) %}

<div class="{{ alert.apply({color, size}, class)|tailwind_merge }}">
     ...
</div>

Compound Variants

You can define compound variants. A compound variant is a variant that applies when multiple other variant conditions are met:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
{% set alert = html_cva(
    base='alert',
    variants={
        color: {
            blue: 'bg-blue',
            red: 'bg-red',
            green: 'bg-green',
        },
        size: {
            sm: 'text-sm',
            md: 'text-md',
            lg: 'text-lg',
        }
    },
    compoundVariants=[{
        // if color = red AND size = (md or lg), add the `font-bold` class
        color: ['red'],
        size: ['md', 'lg'],
        class: 'font-bold'
    }]
) %}

<div class="{{ alert.apply({color, size}) }}">
     ...
</div>

{# index.html.twig #}

{{ include('alert.html.twig', {color: 'red', size: 'lg'}) }}
// class="alert bg-red text-lg font-bold"

{{ include('alert.html.twig', {color: 'green', size: 'sm'}) }}
// class="alert bg-green text-sm"

{{ include('alert.html.twig', {color: 'red', size: 'md'}) }}
// class="alert bg-green text-lg font-bold"

Default Variants

If no variants match, you can define a default set of classes to apply:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{% set alert = html_cva(
    base='alert ',
    variants={
        color: {
            blue: 'bg-blue',
            red: 'bg-red',
            green: 'bg-green',
        },
        size: {
            sm: 'text-sm',
            md: 'text-md',
            lg: 'text-lg',
        },
        rounded: {
            sm: 'rounded-sm',
            md: 'rounded-md',
            lg: 'rounded-lg',
        }
    },
    defaultVariant={
        rounded: 'md',
    }
) %}

<div class="{{ alert.apply({color, size}) }}">
     ...
</div>

{# index.html.twig #}

{{ include('alert.html.twig', {color: 'red', size: 'lg'}) }}
// class="alert bg-red text-lg font-bold rounded-md"

Note

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

This function works best when used with TwigComponent.