Object-Oriented Templates

PHP Classes that render: Twig Components

Create reusable, object-oriented templates. PHP class + template = Twig components.

composer require symfony/ux-twig-component
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent]
class Alert
{
    public string $type = 'success';
    public string $message;

    public function getIcon(): string
    {
        return match ($this->type) {
            'success' => 'bi:check-circle',
            'danger' => 'bi:exclamation-circle',
        };
    }
}
<div class="alert alert-{{ type }} alert-dismissible" role="alert">
    <twig:ux:icon name="{{ this.icon }}" class="Icon bi" />
    {{ message }}
</div>
Symfony logo

UX Twig Components

<twig:Alert message="I am a success alert!" />
<twig:Alert
    type="danger"
    message="Oh no! The dinos escaped!"
/>

Install It

$ composer require symfony/ux-twig-component