TwigBundle
Official Symfony bundle that wires the Twig templating engine into the full-stack framework
Repository Health
Technical Analysis
TwigBundle is the integration layer that connects the standalone Twig templating engine to the Symfony framework’s dependency injection container, HTTP kernel, and console application. Rather than implementing template rendering itself, it configures Twig’s loaders, extensions, and caching behavior from Symfony’s YAML/PHP configuration, registers Twig as the framework’s default view renderer, and exposes twig:lint console commands plus form-theming and translation-extraction integration.
Because it ships as a first-party Symfony component, TwigBundle stays in lockstep with each Symfony major/minor release, tracking the same versioning scheme as symfony/framework-bundle. Most developers touch it only through config/packages/twig.yaml; the bundle itself is what makes $this->render() calls inside a Symfony controller work without any manual Twig environment wiring.
What You Get
- Automatic registration of a fully configured Twig
Environmentservice with Symfony’s path-aware template loader - A
twig.yamlconfiguration schema for globals, paths, form themes, and cache/debug behavior - Console commands (
twig:lint) for validating template syntax across a project - Compiler passes that wire Twig extensions, runtime loaders, and attribute-based filters/functions (
#[AsTwigFilter],#[AsTwigFunction]) into the container - Conditional integration with Symfony Form, Translation, Validator, and AssetMapper components when they’re present
Common Use Cases
- Rendering HTML views from Symfony controllers via
$this->render('template.html.twig') - Theming Symfony Forms with Twig form themes and block overrides
- Linting a project’s Twig templates in CI with the
twig:lintconsole command - Registering custom Twig filters/functions as tagged services for reuse across an application
Under The Hood
Architecture - TwigBundle’s TwigExtension (a Symfony DependencyInjection\Extension) loads PHP-based service definitions from Resources/config and conditionally registers additional service files (form theming, console commands, translation extraction) based on which other Symfony components (symfony/form, symfony/console, symfony/translation, symfony/validator) are present via ContainerBuilder::willBeAvailable(). A set of compiler passes (in DependencyInjection/Compiler) then wires tagged services — Twig extensions, runtime loaders, and attribute-discovered filters/functions/tests annotated with #[AsTwigFilter]/#[AsTwigFunction]/#[AsTwigTest] — into the shared Twig Environment service at container-compile time. Tech Stack - Pure PHP 8.4+, built entirely on Symfony’s own component set (Config, DependencyInjection, HttpFoundation, HttpKernel, Console) plus the standalone symfony/twig-bridge package, which supplies the actual Twig extension classes this bundle only registers. Code Quality - 21 test files under Tests/ exercise the DI extension, configuration schema, and compiler passes with PHPUnit; the codebase follows Symfony’s strict internal coding standards (typed properties, declare(strict_types=1) conventions, PHPDoc author blocks) and every file carries the shared Symfony license header. API Design - Configuration is entirely declarative through config/packages/twig.yaml; application code rarely calls this bundle’s classes directly, instead relying on convention ($this->render() in a controller) and attribute-based service tagging, which keeps the day-to-day developer surface minimal despite the bundle’s internal complexity.
Used by 3 apps in this directory
Kimai
Invoicing Finance · Project Management
Professional open-source time tracking with invoicing, multi-user support, SAML/LDAP auth, and a full REST API—self-host it or use the cloud.
Mautic
Automation · Marketing · Ecommerce
The world's largest open source marketing automation platform — own your data, run multi-channel campaigns, and escape vendor lock-in forever.
wallabag
Bookmarks Archiving
Self-hosted read-it-later app that saves clean, ad-free articles from any webpage for distraction-free reading across all your devices.