Extensions

Besides the translation, the package has these extensions for the templates. For explicit PHP construction, add them in the extensions option of the service. With DI, contribute an extension provider as shown below.

Markdown

Derafu\Twig\Extension\MarkdownExtension has the markdown filter, which renders Markdown with derafu/markdown (install it to use the extension):

{{ text|markdown }}

With true, the Markdown can also have Twig code, which is rendered with the context of the template, and the tags of components, which the Markdown parser does not change:

{{ text|markdown(true) }}

A content that PCRE can not process is a translatable error that says why.

Nested Twig

Derafu\Twig\Extension\TwigExtension has the twig filter, which renders a string as a template with the context of the one that uses it:

{{ 'Hello {{ name }}'|twig }}

The result is not escaped.

Routes

Derafu\Twig\Extension\RoutingExtension has path(), url() and is_active_path(), over a router of derafu/routing (install it to use the extension):

use Derafu\Routing\Router;
use Derafu\Routing\ValueObject\RequestContext;
use Derafu\Twig\Extension\RoutingExtension;

$router = new Router();
$router->addRoute('docs', '/docs', 'handler');
$router->addRoute('post', '/blog/{slug}', 'handler');
$router->setContext(new RequestContext(host: 'example.com', scheme: 'https', pathInfo: '/docs/intro'));

$extension = new RoutingExtension($router);
{{ path('post', {slug: 'hello'}) }}   {# /blog/hello #}
{{ url('docs') }}                     {# https://example.com/docs #}
{{ url('docs', {}, true) }}           {# //example.com/docs #}
{{ is_active_path('docs') }}          {# true: /docs is the start of /docs/intro #}
{{ is_active_path('docs', {}, false) }}   {# false: it is not exactly /docs #}

is_active_path() needs the context of the router (the current path): without it, it is a translatable error. The route reference scanner checks that the routes of a template exist.

Extension Providers

Providers implement Derafu\Twig\Contract\ExtensionProviderInterface. They receive the configuration of the environment being created, so a provider can respect per-environment flags without storing dependencies in configuration arrays. For example:

namespace App\Twig;

use Derafu\Twig\Contract\ExtensionProviderInterface;
use Derafu\Twig\Extension\TwigExtension;

final class ApplicationExtensionProvider implements ExtensionProviderInterface
{
    public function getExtensions(array $options = []): iterable
    {
        yield new TwigExtension();
    }
}

Register the provider as a member of the tagged collection after importing twig-services.yaml:

services:
    App\Twig\ApplicationExtensionProvider:
        tags: ['derafu_twig.extension_provider']

The provider can receive dependencies through its constructor. Providers are visited in tag-priority order (higher priority first). For extensions supplied by providers, the first instance of each extension class wins. Explicit PHP extensions options are registered first and therefore take precedence.

The package registers DefaultExtensionProvider with priority -1000, after consumer providers with normal priority 0. It supplies TranslationExtension using the application’s Symfony\Contracts\Translation\TranslatorInterface when available, followed by the installed extra extensions when extra is true. A consumer-supplied instance of the same extension class is preserved instead of being overwritten or added twice. These providers apply only to environments created by TwigCreator, not to an existing environment passed to TwigService.

On this page

Last updated on 09/11/2026 by Anonymous