This extension requires PHP 8.2+, Twig 3.0+ and Tailwind 3.0+.
You can install the extension using Composer:
composer require tales-from-a-dev/twig-tailwind-extra
If you are using Symfony Flex, this is done automatically, otherwise register the bundle into config/bundles.php
:
// config/bundles.php
return [
// ...
TalesFromADev\Twig\Extra\Tailwind\Bridge\Symfony\Bundle\TalesFromADevTwigExtraTailwindBundle::class => ['all' => true],
];
If you use Twig as standalone, then you need to add the extension manually
$extension = new \TalesFromADev\Twig\Extra\Tailwind\TailwindExtension();
$twig = new \Twig\Environment($loader);
$twig->addRuntimeLoader(new \Twig\RuntimeLoader\FactoryRuntimeLoader([
\TalesFromADev\Twig\Extra\Tailwind\TailwindRuntime::class => fn () => new \TalesFromADev\Twig\Extra\Tailwind\TailwindRuntime(),
]));
$twig->addExtension($extension);
The following filters are available
- TailwindExtension
- tailwind_merge() Integration of TailwindMerge by Sandro Gehri
{{ 'text-red-500 text-blue-500'|tailwind_merge }} // 'text-blue-500'
{{ ['block', 'inline']|tailwind_merge }} // 'inline'
{{ 'tw-text-red-500 tw-text-blue-500'|tailwind_merge({prefix: 'tw-'}) }} // 'tw-text-blue-500'