setono / tag-bag-bundle
A Symfony bundle that integrates the tag bag library into Symfony
Installs: 405 769
Dependents: 12
Suggesters: 0
Security: 0
Stars: 4
Watchers: 2
Forks: 5
Open Issues: 4
Type:symfony-bundle
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-11-21 10:48:19 UTC
README
This bundle integrates the tag bag library and creates a service named
setono_tag_bag.tag_bag
which you can use to inject tags onto pages.
It is especially useful when you want to inject tags that are dynamic by nature. This could be ecommerce tracking, affiliate tracking etc.
It works by adding tags to the tag bag within the request cycle. When the request cycle is done, the remaining tags will be saved to the session. On a new page load, the tag bag will be restored. This is what makes it extremely versatile when you want to track events in your HTML, but the event is happening in a service/controller somewhere.
Installation
Step 1: Download
composer require setono/tag-bag-bundle
Step 2: Enable the bundle
If you use Symfony Flex it will be enabled automatically. Else you need to add it to the config/bundles.php
:
<?php // config/bundles.php return [ // ... Setono\TagBagBundle\SetonoTagBagBundle::class => ['all' => true], // ... ];
Usage
You can autowire the TagBag
like this:
<?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");') ); } }
To output all the tags you've defined, including tags in custom sections, you can use a template like this:
<!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>
Renderers
If you have created your own renderer, remember to tag it setono_tag_bag.renderer
. If you're using autoconfiguration,
it will be tagged automatically.