setono / tag-bag-bundle
一个将标签袋库集成到Symfony的Symfony包
v3.2.0
2024-05-21 09:50 UTC
Requires
- php: >=8.1
- setono/tag-bag: ^2.3
- symfony/config: ^6.4 || ^7.0
- symfony/dependency-injection: ^6.4 || ^7.0
- symfony/event-dispatcher: ^6.4 || ^7.0
- symfony/http-foundation: ^6.4 || ^7.0
- symfony/http-kernel: ^6.4 || ^7.0
- twig/twig: ^2.0 || ^3.0
- webmozart/assert: ^1.11
Requires (Dev)
- matthiasnoback/symfony-dependency-injection-test: ^4.3 || ^5.1
- nyholm/symfony-bundle-test: ^2.0 || ^3.0
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^10.5.20
- psalm/plugin-phpunit: ^0.19
- psalm/plugin-symfony: ^5.1
- setono/code-quality-pack: ^2.7
- symfony/twig-bundle: ^6.4 || ^7.0
Conflicts
- vimeo/psalm: 5.24.0
This package is auto-updated.
Last update: 2024-09-21 10:32:28 UTC
README
此包集成了标签袋库,并创建了一个名为setono_tag_bag.tag_bag
的服务,您可以使用它将标签注入到页面中。
当您想要注入本质上是动态的标签时,它特别有用。这可能是电商跟踪、联盟跟踪等。
它通过在请求周期内向标签袋添加标签来工作。当请求周期完成时,剩余的标签将被保存到会话中。在新的页面加载时,标签袋将被恢复。这就是为什么当您想在HTML中跟踪事件,但事件发生在某个服务/控制器中时,它非常灵活。
安装
步骤 1: 下载
composer require setono/tag-bag-bundle
步骤 2: 启用包
如果您使用Symfony Flex,它将自动启用。否则,您需要将其添加到config/bundles.php
。
<?php // config/bundles.php return [ // ... Setono\TagBagBundle\SetonoTagBagBundle::class => ['all' => true], // ... ];
使用方法
您可以使用以下方式自动注入TagBag
:
<?php use Setono\TagBag\Tag\InlineScriptTag; use Setono\TagBag\TagBagInterface; class YourService { private $tagBag; public function __construct(TagBagInterface $tagBag) { $this->tagBag = $tagBag; } public function method(): void { $this->tagBag->addTag( InlineScriptTag::create('console.log("This will be output in the console");') ); } }
要输出您定义的所有标签,包括自定义部分中的标签,您可以使用以下模板:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{% block title %}Welcome!{% endblock %}</title> {% block stylesheets %}{% endblock %} {{ setono_tag_bag_render_head() }} </head> <body> {{ setono_tag_bag_render_body_begin() }} <h1>This is your page content</h1> <p>Lorem ipsum</p> {{ setono_tag_bag_render_section('custom_section') }} <h2>More page content</h2> <p>Lorem ipsum</p> {{ setono_tag_bag_render_body_end() }} {{ setono_tag_bag_render_all() }} {# This is a catch all that will output the tags that wasn't output before #} </body> </html>
渲染器
如果您创建了您自己的渲染器,请记住将其标记为setono_tag_bag.renderer
。如果您使用自动配置,它将自动标记。