A collection of custom extensions for the Twig template engine.
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 |
The suggested installation method is via composer:
composer require alexmasterov/twig-extension
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)
);