alexmasterov/equip-twig

The Twig integration for the Equip framework

3.1.0 2017-01-07 16:27 UTC

This package is not auto-updated.

Last update: 2024-11-19 14:53:31 UTC


README

Latest Stable Version License Build Status Code Coverage Scrutinizer Code Quality

The Twig integration for the Equip.

Installation

The suggested installation method is via composer:

composer require alexmasterov/equip-twig:^2.0

Configuration

To use the TwigFormatter implementation you need to add TwigResponderConfiguration into the application bootstrap:

Equip\Application::build()
->setConfiguration([
    // ...
    AlexMasterov\EquipTwig\Configuration\TwigResponderConfiguration::class
])
// ...

Setting up the Twig environment:

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"

Usage

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'));
    }
}