/twig-hashtagify

TWIG filter to turn hashtags into html links.

Primary LanguagePHPMIT LicenseMIT

Hashtagify (filter for TWIG)

Total Downloads Latest Stable Version License

TWIG filter to turn hashtags into html links. Provides interfaces to work with a Slim Framework Router, or a base URL.

Installation

  1. Install through composer.
composer require techwilk/twig-hashtagify
  1. Then configure base url using one of the interfaces provided, or write your own:

Base Url Interface:

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));

Slim Router Interface:

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

Use as a standard twig filter:

{{ 'this text with #hashtags' | hashtagify }}

Example

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