PHP code example of setono / tag-bag

1. Go to this page and download the library: Download setono/tag-bag library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

setono / tag-bag example snippets



declare(strict_types=1);

use Setono\TagBag\Renderer\ElementRenderer;
use Setono\TagBag\Tag\InlineScriptTag;
use Setono\TagBag\TagBag;

$tagBag = new TagBag(new ElementRenderer());

// in a controller or service
$tagBag->add(InlineScriptTag::create('trackSomething();'));

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


use Setono\TagBag\Tag\ContentTag;

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


use Setono\TagBag\Tag\ElementTag;

$tag = ElementTag::createWithContent('div', 'content');


use Setono\TagBag\Tag\InlineScriptTag;

$tag = InlineScriptTag::create('alert("Hey!");');


use Setono\TagBag\Tag\InlineScriptTag;

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


use Setono\TagBag\Tag\LinkTag;

$tag = LinkTag::create('stylesheet', 'https://example.com/style.css');


use Setono\TagBag\Tag\StyleTag;

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


use Setono\TagBag\Tag\InlineScriptTag;
use Setono\TagBag\TagBagInterface;

/** @var TagBagInterface $tagBag */

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

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

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