setono/tag-bag

Inject dynamic tags programmatically

v1.4.1 2021-09-09 10:05 UTC

This package is auto-updated.

Last update: 2021-11-09 10:30:37 UTC


README

Latest Version Latest Unstable Version Software License Build Status Coverage Status Quality Score

Tag bag is an object oriented and very extendable way of adding content/tags to your pages.

A very common use case for the tag bag is tracking events on your pages.

Installation

$ composer require setono/tag-bag

Basic usage

<?php
use Setono\TagBag\Tag\ScriptTag;
use Setono\TagBag\TagBagInterface;

/** @var TagBagInterface $tagBag */

// in a controller or service
$tagBag->addTag(new ScriptTag('trackSomething();'));

// in your template
$tagBag->renderAll();

The above call to TagBagInterface::renderAll() would output the following:

<script>trackSomething();</script>

Here we introduced two important concepts of the tag bag: The tags and the rendering of the tags.

Tags are PHP classes implementing the TagInterface and they are designed to make it easier for you to output content on your pages. The ones included are pretty basic, and you may find that you'd want to use some of the other more advanced tags that you can read about in the tags section below.

Tags

The base library has three tags and on top of that you can use other tags by installing small sub packages of the tag bag.

Content tag

<?php
use Setono\TagBag\Tag\ContentTag;

$tag = new ContentTag('<div class="class-name">tag</div>');

Renders as:

<div class="class-name">tag</div>

Script tag

<?php
use Setono\TagBag\Tag\ScriptTag;

$tag = new ScriptTag('alert("Hey!");');

Renders as:

<script type="application/ld+json">
alert("Hey!");
</script>

The script tag also has an optional property named type:

<?php
use Setono\TagBag\Tag\ScriptTag;

$tag = new ScriptTag('{"@context": "https://schema.org/"}');
$tag->setType('application/ld+json');

The above renders as:

<script type="application/ld+json">
{"@context": "https://schema.org/"}
</script>

Style tag

<?php
use Setono\TagBag\Tag\StyleTag;

$tag = new StyleTag('body { background-color: red; }');

Renders as:

<style>
body { background-color: red; }
</style>

Twig tag

Render using twig templates. See installation instructions and usage here.

PHP templates tag

Render using PHP templates. See installation instructions and usage here.

Gtag tag

If you're using Google's services, some of them allow you to track events using the gtag.

To make it easier to create these tags, you can use the gtag extension for the tag bag.

Renderers

The base library contains three renderers that corresponds to the base tags. A renderer implements the RendererInterface.

Just as with the tags there are also renderers in the sub packages.

Content renderer

The ContentRenderer renders the content you've input in the tag.

Script renderer

The ScriptRenderer wraps the content in a <script> tag.

Style renderer

The StyleRenderer wraps the content in a <style> tag.

Storage

The intended use of the tag bag is to save the tag bag upon end of request and restore it upon starting the request life cycle. The TagBagInterface has store and restore methods for these events respectively.

<?php
use Setono\TagBag\Tag\ScriptTag;
use Setono\TagBag\TagBagInterface;

/** @var TagBagInterface $tagBag */

// in a controller or service
$tagBag->addTag(new ScriptTag('trackSomething();'));

// this stores the contents of the tag bag
$tagBag->store();

// this restores the contents of the tag bag
$tagBag->restore();

Framework integration