alexmasterov/twig-extension

A collection of custom extensions for the Twig template engine

0.4.0 2016-09-24 23:13 UTC

This package is not auto-updated.

Last update: 2024-04-09 14:37:51 UTC


README

Latest Stable Version License Build Status Code Coverage Scrutinizer Code Quality

A collection of custom extensions for the Twig template engine.

Extensions

Psr7UriExtension — provides useful functions using PSR-7 HTTP message interface.

Function Description
absolute_url Generate an absolute URL for the given absolute or relative path
relative_path Generate a relative path based on the current path of the URI

Installation

The suggested installation method is via composer:

composer require alexmasterov/twig-extension

Configuration

To activate an extension you need to register it into the Twig environment:

/*
* @var $twig    Twig_Environment
* @var $request ServerRequestInterface
*/
$twig->addExtension(
    new AlexMasterov\TwigExtension\Psr7UriExtension(ServerRequestInterface $request)
);

The example of registering the extension using Auryn and Diactoros:

$injector = new Auryn\Injector;
$injector->alias(
    Psr\Http\Message\ServerRequestInterface::class,
    Zend\Diactoros\ServerRequest::class
);

/*
* @var $twig Twig_Environment
*/
$twig->addExtension(
    $injector->make(AlexMasterov\TwigExtension\Psr7UriExtension::class)
);