Linkify Twig extension
Use Linkify in your twig template., (*1)
Installation
Using Composer:
composer require manuelodelain/linkify-twig-extension, (*2)
Usage
use manuelodelain\Twig\Extension\LinkifyExtension;
use Twig\Environment;
$twig = new Environment(...);
$twig->addExtension(new LinkifyExtension());
{{ 'Lorem ipsum ... www.website.com ...'|linkify }}
Will output:, (*3)
Lorem ipsum ... <a href="www.website.com">www.website.com</a> ...
Don't forget to apply the raw filter for an HTML output:, (*4)
{{ 'Lorem ipsum ... www.website.com ...'|linkify|raw }}
Options
As Linkify, set default options at the instanciation or at the method call., (*5)
At the instanciation (applied to all links):, (*6)
use manuelodelain\Twig\Extension\LinkifyExtension;
$twig = new Environment(...);
$twig->addExtension(new LinkifyExtension(array('attr' => array('target' => '_blank'))));
At the method call:, (*7)
{{ 'Lorem ipsum ... www.website.com ...'|linkify({"attr": {"target": "_blank"}}) }}
Will output:, (*8)
Lorem ipsum ... <a href="www.website.com" target="_blank">www.website.com</a> ...
See the Linkify options, (*9)