Derafu

derafu/twig creates a Twig environment without Symfony and registers Symfony UX Twig Components in it. This is how the components are used in a Derafu site.

composer require derafu/twig derafu/twig-components

derafu/twig-components keeps derafu/twig optional. Applications using Derafu Twig can load the integration YAML below to contribute the components and their templates to the shared environment. Symfony’s native integration uses the main component configuration without loading this bridge.

Directly

Give the environment the templates of the package in paths, and the components in components. The directory of the classes has all of them:

use Derafu\Twig\Provider\DirectoryComponentProvider;
use Derafu\Twig\Service\TwigService;

$vendor = __DIR__ . '/vendor/derafu/twig-components';

$twig = new TwigService([
    'paths' => [__DIR__ . '/templates', $vendor . '/resources/templates'],
    'components' => new DirectoryComponentProvider($vendor . '/src/Component'),
]);

$data = [];

echo $twig->renderFromString('<!--TWIG_BLOCK_0-->', $data);

The components option also takes the classes of the components, or their instances (see Components).

With the Container

Import Twig’s shared services and the optional integration:

imports:
    - { resource: '../vendor/derafu/twig/resources/config/twig-services.yaml' }
    - { resource: '../vendor/derafu/twig-components/resources/config/twig-components-twig-services.yaml' }

The optional file imports twig-components-services.yaml, which registers the component classes and translation resource provider. It also registers Derafu\TwigComponents\Bridge\DerafuTwig\TwigComponentsTemplatePathProvider with derafu_twig.template_path_provider. The provider contributes this package’s template directory; applications do not need to repeat that path.

Inject Derafu\Twig\Contract\TwigServiceInterface into consumers. Components render in that shared service’s environment. Register the application’s translator under Symfony\Contracts\Translation\TranslatorInterface and pass !tagged_iterator derafu_translation.resource_provider to the translation factory to include the component catalogues.

Application template-path providers can override individual component templates with a higher tag priority. The package’s other templates remain available. For example, contribute an application directory containing components/block-alert.html.twig to replace only the alert template.

With the Renderer

Renderer imports the shared Twig configuration. Add the component integration alongside it:

imports:
    - { resource: '../vendor/derafu/renderer/resources/config/renderer-services.yaml' }
    - { resource: '../vendor/derafu/twig-components/resources/config/twig-components-twig-services.yaml' }

Inject Derafu\Renderer\Contract\RendererInterface and render components through its shared Twig engine:

echo $renderer->renderFromString('<!--TWIG_BLOCK_1-->');

Both configurations contribute to the same Twig service. The optional bridge does not create an environment or a renderer of its own.

On this page

Last updated on 09/11/2026 by Anonymous