Twig Environment, Extensions and Lint Tools for PHP
Derafu Twig creates a Twig environment that is ready to use in any PHP code, with or without a framework, and gives it the extensions and the tools that the Derafu libraries use.
Features
- 🌿 A Twig environment in one call:
TwigServiceandTwigCreatorcreate it from its paths, extensions and components. - 🧩 Symfony UX Twig Components without Symfony: the components of your application (or of a library such as
derafu/twig-components) are registered in the environment (see Components). - 🌐 Translation: the
transfilter, thet()function and the tags {% verbatim %}{% trans %}{% endverbatim %} and {% verbatim %}{% trans_default_domain %}{% endverbatim %}, withoutsymfony/twig-bridge(see Translations). - 📝 Markdown, routes and nested Twig: the filters
markdownandtwigand the functionspath(),url()andis_active_path()(see Extensions). - 🔎 Lint tools: they read the templates to find the routes that do not exist and the texts that are not translated (see Lint).
- 🗄️ PSR-6 cache: the configuration of the components is cached in any PSR-6 pool.
- 🏷️ MIT License: open-source and free to use.
Installation
composer require derafu/twig
Basic Usage
use Derafu\Twig\Service\TwigService;
$twig = new TwigService([
'paths' => [__DIR__ . '/templates'],
]);
$data = ['name' => 'World'];
echo $twig->render('hello', $data); // Renders templates/hello.html.twig.
echo $twig->renderFromString('Hello {% verbatim %}{{ name }}{% endverbatim %}', $data); // Renders a string.
The service, its options and its defaults are in Twig Service.
Where to Go Next
- Twig Service: create the environment and render with it.
- Components: register Symfony UX Twig Components.
- Extensions: the filters and the functions of the templates.
- Translations: translate the templates.
- Lint: check the templates in the tests.
On this page
Last updated on 30/10/2026
by Anonymous