Integrates Twig with PHPNomad's template strategy. It implements phpnomad/template's CanRender interface on top of Twig 3, so any code in your application that depends on CanRender can render .twig files without knowing Twig is doing the work. This is the default templating integration for PHPNomad applications that run outside WordPress.
composer require phpnomad/twig-integrationTwigEngine, aCanRenderimplementation that loads templates through Twig'sEnvironmentandFilesystemLoaderand auto-appends.twigto the template path you pass in.TwigConfigProvider, an interface your application implements to tell the engine where its template directory lives.- Exception mapping so a missing template surfaces as
TemplateNotFoundand any other render failure surfaces asTemplateException, both fromphpnomad/template.
- PHP 8.0 or later (inherited from
twig/twig ^3.0) phpnomad/template ^1.0twig/twig ^3.0
Implement TwigConfigProvider to point at your templates directory, then bind TwigEngine to CanRender in your bootstrapper's class definitions.
<?phpnamespaceMyApp\Templates;
usePHPNomad\Twig\Integration\Interfaces\TwigConfigProvider;
class TemplateConfig implements TwigConfigProvider
{
publicfunctiongetTemplateDirectory(): string
{
return__DIR__ . '/templates';
}
}<?phpnamespaceMyApp;
useMyApp\Templates\TemplateConfig;
usePHPNomad\Loader\Interfaces\HasClassDefinitions;
usePHPNomad\Template\Interfaces\CanRender;
usePHPNomad\Twig\Integration\Interfaces\TwigConfigProvider;
usePHPNomad\Twig\Integration\Strategies\TwigEngine;
class TemplateInitializer implements HasClassDefinitions
{
publicfunctiongetClassDefinitions(): array
{
return [
TwigEngine::class => CanRender::class,
TemplateConfig::class => TwigConfigProvider::class,
];
}
}With those bindings in place, anywhere CanRender is injected you can call $template->render('emails/welcome', ['name' => $user->name]) and the engine will resolve templates/emails/welcome.twig.
- Bootstrapping and class definitions: phpnomad.com
- Twig template syntax and features: twig.symfony.com
MIT. See LICENSE.txt.