TWIG filter to turn hashtags into html links. Provides interfaces to work with a Slim Framework Router, or a base URL.
- Install through composer.
composer require techwilk/twig-hashtagify
- Then configure base url using one of the interfaces provided, or write your own:
use TechWilk\TwigHashtagify\HashtagifyUrlGenerator\BaseUrlHashtagifyUrlGenerator;
use TechWilk\TwigHashtagify\HashtagifyExtension;
use TechWilk\TwigHashtagify\HashtagifyRuntimeLoader;
$urlGenerator = new BaseUrlHashtagifyUrlGenerator('https://example.com/hashtag/');
$twig->addExtension(new HashtagifyExtension());
$twig->addRuntimeLoader(new HashtagifyRuntimeLoader($urlGenerator));
Add the following extension as a twig dependency
use TechWilk\TwigHashtagify\HashtagifyExtension;
$twig->addExtension(new HashtagifyExtension());
Add the following middleware after your TwigMiddleware
to setup our dependencies
use TechWilk\TwigHashtagify\HashtagifyMiddleware;
$app->add(HashtagifyMiddleware::createFromContainer($app, 'route-name', 'argument-name'));
Use as a standard twig filter:
{{ 'this text with #hashtags' | hashtagify }}
Turns this:
This is some text with #hashtags in it
Into this:
This is some text with <a href="http://example.com/hashtag/hashtags">#hashtags</a> in it