Symfony
Use Twig Components Bundle to register the components, their templates and their translations in Symfony’s existing Twig environment:
composer require derafu/symfony-twig-components-bundle
Follow the bundle’s installation instructions to enable its bundles and Symfony’s translator. It registers the components and fallback template paths, and uses Translation Bundle to load the tagged translation resources. No manual component service registration is needed.
The integration does not require derafu/twig, Renderer or Content. Application
Twig extensions and Symfony UX components continue to use the same environment.
Styles and scripts remain part of the application’s asset setup.
Manual Integration
The following configuration is an alternative for applications that register the library directly without Twig Components Bundle. Use either the bundle or this manual registration, avoiding duplicate component and translation registrations.
Requirements
symfony/framework-bundleandsymfony/twig-bundle.symfony/ux-twig-component, with its bundle enabled (its recipe does it when you install it).
composer require derafu/twig-components
The templates
The templates of the components are in the components directory of resources/templates. Give that path to Twig, and Symfony UX finds them:
# config/packages/twig.yaml
twig:
paths:
'%kernel.project_dir%/vendor/derafu/twig-components/resources/templates': ~
The translations
The texts of the components are translated in the catalogues of resources/translations. Give that path to the translator:
# config/packages/translation.yaml
framework:
translator:
paths:
- '%kernel.project_dir%/vendor/derafu/twig-components/resources/translations'
The catalogues are in Spanish (the texts of the components are their English ids). Set the locale of your application to use them:
# config/packages/framework.yaml
framework:
default_locale: es
The components
The classes of the components are services that Symfony UX discovers by their #[AsTwigComponent] attribute. Register the directory with autoconfigure:
# config/services.yaml
services:
_defaults:
autowire: true
autoconfigure: true
Derafu\TwigComponents\Component\:
resource: '../vendor/derafu/twig-components/src/Component/'
Use
With the bundle or the three manual settings, the tags work in any template of the application:
<twig:block-alert type="success" content="Saved." />
The page also needs the styles and the scripts of Bootstrap and Font Awesome, and the CSS of the components: see Styles.
Strict Mode
The templates are written for strict_variables, the mode that Symfony has in dev: they read the optional keys of an item in a way that does not fail when the key is not there. The tests of the package render every component and every example of this documentation in that mode, in a Symfony kernel.