techwilk/twig-hashtagify

将标签转换为HTML链接的TWIG过滤器。

v3.0.1 2024-09-10 19:27 UTC

README

Total Downloads Latest Stable Version License

将标签转换为HTML链接的TWIG过滤器。提供与Slim框架路由器或基本URL一起工作的接口。

安装

  1. 通过composer安装。
composer require techwilk/twig-hashtagify
  1. 然后使用提供的接口之一配置基本URL,或者编写自己的

基本URL接口

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路由器接口

将以下扩展作为TWIG依赖项添加

use TechWilk\TwigHashtagify\HashtagifyExtension;

$twig->addExtension(new HashtagifyExtension());

在您的TwigMiddleware之后添加以下中间件来设置我们的依赖项

use TechWilk\TwigHashtagify\HashtagifyMiddleware;

$app->add(HashtagifyMiddleware::createFromContainer($app, 'route-name', 'argument-name'));

使用

作为标准TWIG过滤器使用

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

示例

将这个转换为

This is some text with #hashtags in it

变成这个

This is some text with <a href="http://example.com/hashtag/hashtags">#hashtags</a> in it