1. Go to this page and download the library: Download tobento/service-tag 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/ */
tobento / service-tag example snippets
use Tobento\Service\Tag\Tag;
use Tobento\Service\Tag\TagInterface;
$tag = new Tag(
name: 'p',
html: 'html' // Must be escaped
);
var_dump($tag instanceof TagInterface);
// bool(true)
use Tobento\Service\Tag\Tag;
use Tobento\Service\Tag\Attributes;
$tag = new Tag(
name: 'li',
html: 'html', // Must be escaped
attributes: new Attributes(), // is default
level: 2, // default is null
renderEmptyTag: false, // default is true
);
use Tobento\Service\Tag\TagFactory;
use Tobento\Service\Tag\TagFactoryInterface;
$tagFactory = new TagFactory();
var_dump($tagFactory instanceof TagFactoryInterface);
// bool(true)
use Tobento\Service\Tag\TagFactory;
use Tobento\Service\Tag\TagInterface;
use Tobento\Service\Tag\Attributes;
$tag = (new TagFactory())->createTag(
name: 'p',
html: '', // is default
attributes: new Attributes(), // is default
level: 2, // default is null
);
var_dump($tag instanceof TagInterface);
// bool(true)
use Tobento\Service\Tag\TagFactory;
use Tobento\Service\Tag\TagInterface;
$tag = (new TagFactory())->createTagFromHtml(html: 'html');
var_dump($tag instanceof TagInterface);
// bool(true)
use Tobento\Service\Tag\Tag;
use Tobento\Service\Tag\TagInterface;
use Tobento\Service\Tag\AttributesInterface;
$tag = new Tag(name: 'li', html: 'html');
var_dump($tag instanceof TagInterface);
// bool(true)
var_dump($tag->getName());
// string(2) "li"
var_dump($tag->getHtml());
// string(4) "html"
var_dump($tag->getLevel());
// NULL (or int)
var_dump($tag->attributes() instanceof AttributesInterface);
// bool(true)
use Tobento\Service\Tag\Tag;
use Tobento\Service\Tag\Attributes;
$tag = new Tag(name: 'li', html: 'html');
$tag = $tag->withName('ul');
$tag = $tag->withHtml('html');
$tag = $tag->withLevel(2);
$tag = $tag->withAttributes(new Attributes());
$tag = $tag->withAttr(name: 'data-foo', value: 'bar');
use Tobento\Service\Tag\Tag;
$tag = new Tag(name: 'li', html: 'html');
$tag->prepend(html: 'foo');
var_dump($tag->getHtml());
string(7) "foohtml"
$tag->append(html: 'bar');
var_dump($tag->getHtml());
// string(10) "foohtmlbar"
use Tobento\Service\Tag\Tag;
$tag = new Tag(name: 'li', html: 'html');
$tag->class(value: 'bar');
$tag->class(value: 'foo');
var_dump(htmlspecialchars((string)$tag));
// string(51) "<li class="bar foo">html</li>"
use Tobento\Service\Tag\Tag;
$tag = new Tag(name: 'p', html: 'html');
<?= $tag->render()
use Tobento\Service\Tag\Tag;
$tag = new Tag(
name: 'p',
html: '',
renderEmptyTag: false
);
var_dump($tag->render());
// string(0) ""
use Tobento\Service\Tag\Tag;
$tag = new Tag(name: 'p', html: 'html');
var_dump(htmlspecialchars($tag->open()));
// string(9) "<p>"
if (!$tag->isSelfClosing()) {
var_dump(htmlspecialchars($tag->close()));
// string(10) "</p>"
}
use Tobento\Service\Tag\NullTag;
use Tobento\Service\Tag\TagInterface;
$tag = new NullTag(
html: 'html', // Must be escaped
level: 2, // default is null
);
var_dump($tag instanceof TagInterface);
// bool(true)
var_dump((string)$tag);
// string(4) "html"
use Tobento\Service\Tag\Taggable;
use Tobento\Service\Tag\HasTag;
use Tobento\Service\Tag\Tag;
use Tobento\Service\Tag\TagInterface;
class Foo implements Taggable
{
use HasTag;
}
$foo = new Foo();
$foo->setTag(new Tag(name: 'p'));
var_dump($foo->tag() instanceof TagInterface);
// bool(true)
use Tobento\Service\Tag\Attributes;
use Tobento\Service\Tag\AttributesInterface;
$attributes = new Attributes();
var_dump($attributes instanceof AttributesInterface);
// bool(true)
// or
$attributes = new Attributes([
'class' => 'name',
'data-foo' => '',
// set null as value or an int key
// as only to render the name:
'
use Tobento\Service\Tag\Attributes;
use Tobento\Service\Tag\AttributesInterface;
$attributes = new Attributes();
var_dump($attributes instanceof AttributesInterface);
// bool(true)
var_dump($attributes->empty());
// bool(true)
$attributes = new Attributes(['class' => 'foo']);
var_dump($attributes->empty());
// bool(false)
use Tobento\Service\Tag\Attributes;
$attributes = new Attributes(['class' => 'foo']);
var_dump($attributes->has('id'));
// bool(false)
var_dump($attributes->has('class'));
// bool(true)