symfony/ux-twig-component
Symfony UX Twig Components lets you bind PHP objects to Twig templates to build reusable UI pieces like alerts, modals, and sidebars. Create small, composable components with clean rendering and better template organization for Symfony apps.
PreMount, PostRender, etc.) for extensibility.provide/inject for dependency injection across nested components, reducing prop drilling and improving scalability.@symfony/stimulus-bridge (v3+) for interactive components. Requires Stimulus v3 if using interactivity.ComponentInterface → AsTwigComponent attribute) are versioned and documented.PreCreateForRenderEvent renames, ComponentAttributes API changes).#[AsTwigComponent]), Twig’s new HTML syntax (<twig:ComponentName>), and component lifecycle events.TemplateCacheWarmer) ensures components are compiled once.ComponentAttributes now handles escaping by default (security fix in v2.25.0).admin/, public/) be organized to avoid collisions?@symfony/stimulus-bridge already in use?provide/inject for state management (e.g., theme context, user auth)?debug:twig-component command and profiler integration be leveraged?RenderedComponent::crawler()) fit into the CI pipeline?profiler.collect_components) be enabled in production?composer.json constraints are updated:
"require": {
"symfony/ux-twig-component": "^3.0",
"symfony/stimulus-bridge": "^3.0" // if using interactivity
}
symfony/twig-bundle:^5.4 or symfony/twig:^3.9).composer require symfony/stimulus-bridge
npm install @hotwired/stimulus @symfony/stimulus-bridge
StimulusAttributes.@symfony/mercure-bundle.{% include 'alert.html.twig' %} with {% component('alert', { message: '...' }) %}).provide/inject for global state (e.g., theme, user preferences).TwigComponentBundle in config/bundles.php:
return [
Symfony\UX\TwigComponent\TwigComponentBundle::class => ['all' => true],
];
config/packages/twig_component.yaml:
twig_component:
defaults:
namespaces: ['@App/Components']
{# Before #}
{% include 'components/alert.html.twig' with { type: 'success', message: 'Done!' } %}
{# After #}
{% component('alert', { type: 'success', message: 'Done!' }) %}
<twig:alert type="success" message="Done!" />
<twig:modal attributes.defaults(stimulus_controller('modal'))>
<!-- Content -->
</twig:modal>
import { Controller } from '@hotwired/stimulus';
export default class extends Controller {
connect() { /* ... */ }
}
template argument in AsTwigComponent to override default paths.{% component('FOSUserBundle:Component/ProfileCard') %}).@AcmeDashboard/Components/Chart).symfony/ux-live-component for real-time updates).provide/inject for shared state.PreMount, PostRender).AsTwigComponent attribute (no manual service registration in Symfony 5.3+).debug:twig-component to list and inspect components:
php bin/console debug:twig-component
How can I help you explore Laravel packages today?