sympress / twig-bundle
Twig integration bundle for SymPress with Symfony TwigBundle feature parity.
Requires
- php: ^8.5
- symfony/config: ^8.0
- symfony/dependency-injection: ^8.0
- symfony/http-foundation: ^8.1
- symfony/http-kernel: ^8.1
- symfony/twig-bridge: ^8.1
- symfony/twig-bundle: ^8.1
- sympress/framework-bundle: dev-main
- sympress/kernel: dev-main
- twig/twig: ^3.22
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: ^1.0
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
- sympress/coding-standards: dev-main
Suggests
- symfony/asset: Enables the asset() Twig function.
- symfony/asset-mapper: Enables importmap Twig functions.
- symfony/form: Enables Symfony form rendering in Twig.
- symfony/html-sanitizer: Enables HTML sanitizer Twig filters.
- symfony/mailer: Enables email body rendering with Twig.
- symfony/routing: Enables path() and url() Twig functions.
- symfony/serializer: Enables serializer Twig helpers.
- symfony/translation: Enables trans and translation extraction support.
- symfony/validator: Enables form/validator integration.
- symfony/web-link: Enables preload/prefetch link helpers.
- symfony/workflow: Enables workflow Twig helpers.
- symfony/yaml: Enables yaml_encode and yaml_dump Twig helpers.
README
sympress/twig-bundle integrates Twig into the SymPress kernel while delegating the
core feature set to Symfony's symfony/twig-bundle.
Installation
composer require sympress/twig-bundle
The bundle is discovered by the SymPress kernel through the package metadata in
composer.json. It requires PHP 8.5, SymPress Kernel, SymPress FrameworkBundle,
Symfony TwigBundle, and Twig 3.
Features
- Symfony TwigBundle compiler passes and service definitions
- Symfony Twig configuration under the
twigextension alias - Template lookup in project
templates/, bundleResources/views, and bundletemplates - Autoconfiguration for Twig extensions, loaders, runtimes, and Twig attributes
- Injectable
TemplateRendererInterface - Optional SymPress API for service-backed Twig globals
Configuration
Twig configuration uses Symfony's standard twig extension alias:
twig: default_path: '%kernel.project_dir%/templates' strict_variables: '%kernel.debug%'
The bundle loads its own service wiring from Resources/config/services.yaml and
keeps Symfony TwigBundle service definitions intact. Project services can keep using
normal Twig extension, loader, runtime, and attribute APIs.
Twig Extensions
Services implementing Twig's extension, loader, or runtime interfaces are autoconfigured the same way as in Symfony:
use Twig\Extension\AbstractExtension; use Twig\TwigFunction; final class ShopTwigExtension extends AbstractExtension { public function getFunctions(): array { return [ new TwigFunction('shop_name', fn (): string => 'SymPress'), ]; } }
Twig attributes from twig/twig are supported as well:
use Twig\Attribute\AsTwigFunction; final class ShopTwigRuntime { #[AsTwigFunction('shop_name')] public function shopName(): string { return 'SymPress'; } }
Twig Globals
Services can provide several globals by implementing GlobalProviderInterface:
use SymPress\TwigBundle\Extension\GlobalProviderInterface; final class AppGlobals implements GlobalProviderInterface { public function getGlobals(): iterable { return [ 'app_name' => 'SymPress', ]; } }
For one service-backed global, tag a service class with AsTwigGlobal:
use SymPress\TwigBundle\Attribute\AsTwigGlobal; #[AsTwigGlobal('current_account')] final class CurrentAccount { }
Rendering
Inject SymPress\TwigBundle\Renderer\TemplateRendererInterface when a service
needs view rendering without extending a controller base class.
use SymPress\TwigBundle\Renderer\TemplateRendererInterface; final readonly class PageRenderer { public function __construct( private TemplateRendererInterface $templates, ) { } public function render(): string { return $this->templates->render('page.html.twig', [ 'title' => 'Hello SymPress', ]); } }
Controllers can use TwigResponseTrait when they need a Symfony Response from
a Twig template without coupling to a concrete renderer implementation.
Quality Checks
composer qa
The QA command runs PHPCS, PHPStan, and PHPUnit with the package configuration used by the GitHub Actions workflow.