The Twig integration for the Equip.
The suggested installation method is via composer:
composer require alexmasterov/equip-twig:^2.0
To use the TwigFormatter
implementation you need to add TwigResponderConfiguration
into the application bootstrap:
Equip\Application::build()
->setConfiguration([
// ...
AlexMasterov\EquipTwig\Configuration\TwigResponderConfiguration::class
])
// ...
Default configuration, via dependency injector:
// src/Configuration/TwigConfiguration.php
namespace Acme\Configuration;
use Auryn\Injector;
use Equip\Env;
use Equip\Configuration\ConfigurationInterface;
use AlexMasterov\EquipTwig\Configuration\TwigResponderConfiguration;
class TwigConfiguration implements ConfigurationInterface
{
public function apply(Injector $injector)
{
$twigEnv = new Env([
'TWIG_TEMPLATES' => __DIR__.'/../Resources/templates',
'TWIG_CACHE' => __DIR__.'/../../var/cache/twig',
'TWIG_DEBUG' => false,
'TWIG_AUTO_RELOAD' => true,
'TWIG_STRICT_VARIABLES' => false,
'TWIG_FILE_EXTENSIONS' => 'html.twig,twig'
]);
$injector->define(TwigResponderConfiguration::class, [
':env' => $twigEnv
]);
}
}
Equip\Application::build()
->setConfiguration([
// ...
Acme\Configuration\TwigConfiguration::class,
AlexMasterov\EquipTwig\Configuration\TwigResponderConfiguration::class
])
// ...
Optional configuration, via a .env
file:
TWIG_TEMPLATES = "../Resources/templates"
TWIG_CACHE = "../var/cache/twig"
TWIG_DEBUG = false
TWIG_AUTO_RELOAD = true
TWIG_STRICT_VARIABLES = false
TWIG_FILE_EXTENSIONS = "html.twig,twig"
Basic example:
namespace Acme\Domain;
use Equip\Adr\DomainInterface;
use Equip\Adr\PayloadInterface;
class WidgetDomain implements DomainInterface
{
/**
* @var PayloadInterface
*/
private $payload;
public function __construct(PayloadInterface $payload)
{
$this->payload = $payload;
}
public function __invoke(array $input)
{
return $this->payload
->withStatus(PayloadInterface::STATUS_OK)
->withSetting('template', 'widget.html.twig')
->withOutput([
'message' => 'Just do it!'
]);
}
}
Using PayloadRenderTrait
as wrapper for the usual render
method:
// ...
use AlexMasterov\EquipTwig\Payload\PayloadRenderTrait;
class WidgetDomain implements DomainInterface
{
use PayloadRenderTrait;
public function __invoke(array $input)
{
$message = 'Just do it!';
return $this->render('widget.html.twig', compact('message'));
}
}