antidot-fw/twig-template-renderer

0.2.0 2021-01-10 14:35 UTC

README

Latest Version on Packagist Software License Build Status Coverage Status Total Downloads

Twig Template Renderer for Antidot Framework

Install

Via Composer

$ composer require antidot-fw/twig-template-renderer

Antidot framework

It will work out of the box, the only thing you need is to inject the TemplateRenderer interface in your request handler constructor

As standalone component

See factory classes at src/Container.

Config

<?php

declare(strict_types=1);

$config = [
    'template' => [
        'debug' => false,
        'file_extension' => 'twig',
        'charset' => 'utf-8',
        'template_path' => 'templates',
        'cache' => 'var/cache/twig',
        'auto_reload' => false,
        'autoescape' => 'html',
        'strict_variables' => true,
        'globals' => [
            // 'name' => 'value',
        ],
        'extensions' => [
            // EtensionClassName::class,
        ],
        'filters' => [
            // 'name' => PHPCallableClass::class,
            // 'some_function' => 'php_some_function,
        ],
        'functions' => [
            // 'name' => PHPCallableClass::class,
            // 'some_function' => 'php_some_function,
        ],
    ],
];

Usage

See full Twig documentation for more detail.

In a request handler

<?php

declare(strict_types=1);

use Antidot\Render\TemplateRenderer;
use Laminas\Diactoros\Response\HtmlResponse;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

class SomeHandler implements RequestHandlerInterface
{
    private TemplateRenderer $template;

    public function __construct(TemplateRenderer $template) 
    {
        $this->template = $template;
    }

    public function handle(ServerRequestInterface $request) : ResponseInterface
    {
        return new HtmlResponse(
            $this->template->render('index.html', [
                'name' => 'Koldo ;-D',
            ])
        );
    }
}

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email kpicaza@example.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.