setono/tag-bag-bundle

一个将标签袋库集成到Symfony的Symfony包

安装量: 394,912

依赖者: 12

建议者: 0

安全性: 0

星标: 4

关注者: 2

分支: 4

开放问题: 3

类型:symfony-bundle

v3.2.0 2024-05-21 09:50 UTC

README

Latest Version Software License Build Status

此包集成了标签袋库,并创建了一个名为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。如果您使用自动配置,它将自动标记。

使用Tag Bag Bundle的项目