PHPackages                             tobento/service-tag - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. tobento/service-tag

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

tobento/service-tag
===================

Creating HTML tags with attributes easily.

2.0(7mo ago)035310MITPHPPHP &gt;=8.4

Since Oct 14Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/tobento-ch/service-tag)[ Packagist](https://packagist.org/packages/tobento/service-tag)[ Docs](https://www.tobento.ch)[ RSS](/packages/tobento-service-tag/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelog (7)Dependencies (2)Versions (9)Used By (10)

Tag Service
===========

[](#tag-service)

HTML tags for PHP applications.

Table of Contents
-----------------

[](#table-of-contents)

- [Getting started](#getting-started)
    - [Requirements](#requirements)
    - [Highlights](#highlights)
- [Documentation](#documentation)
    - [Tag](#tag)
        - [Create Tag](#create-tag)
        - [Tag Factory](#tag-factory)
        - [Tag Interface](#tag-interface)
    - [NullTag](#nulltag)
    - [Taggable](#taggable)
    - [Attributes](#attributes)
        - [Create Attributes](#create-attributes)
        - [Attributes Interface](#attributes-interface)
    - [Str](#str)
- [Credits](#credits)

---

Getting started
===============

[](#getting-started)

Add the latest version of the tag service project running this command.

```
composer require tobento/service-tag

```

Requirements
------------

[](#requirements)

- PHP 8.4 or greater

Highlights
----------

[](#highlights)

- Framework-agnostic, will work with any project
- Decoupled design

Documentation
=============

[](#documentation)

Tag
---

[](#tag)

### Create Tag

[](#create-tag)

```
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)
```

You might adjust the default parameters.

```
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
);
```

### Tag Factory

[](#tag-factory)

Easily create a tag with the provided tag factory:

```
use Tobento\Service\Tag\TagFactory;
use Tobento\Service\Tag\TagFactoryInterface;

$tagFactory = new TagFactory();

var_dump($tagFactory instanceof TagFactoryInterface);
// bool(true)
```

**createTag**

```
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)
```

**createTagFromHtml**

Create a tag from the specified html.

```
use Tobento\Service\Tag\TagFactory;
use Tobento\Service\Tag\TagInterface;

$tag = (new TagFactory())->createTagFromHtml(html: 'html');

var_dump($tag instanceof TagInterface);
// bool(true)
```

### Tag Interface

[](#tag-interface)

**getters**

```
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)
```

**with methods**

The with methods will return a new instance.

```
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');
```

**manipulation methods**

```
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"
```

**attributes methods**

The attr method will overwrite attributes with same names.

```
use Tobento\Service\Tag\Tag;

$tag = new Tag(name: 'li', html: 'html');

$tag->attr(name: 'data-foo', value: 'bar');
$tag->attr(name: 'data-foo', value: 'foo');

var_dump(htmlspecialchars((string)$tag));
// string(50) "html"
```

```
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) "html"
```

**render methods**

```
use Tobento\Service\Tag\Tag;

$tag = new Tag(name: 'p', html: 'html');

// or just

```

Sometimes, it might be useful not rendering the tag if it is empty. You may do so if you set render empty tag to false, it returns an empty string if there is no html.

```
use Tobento\Service\Tag\Tag;

$tag = new Tag(
    name: 'p',
    html: '',
    renderEmptyTag: false
);

var_dump($tag->render());
// string(0) ""
```

You may want to do the opening and closing by yourself.

```
use Tobento\Service\Tag\Tag;

$tag = new Tag(name: 'p', html: 'html');

var_dump(htmlspecialchars($tag->open()));
// string(9) ""

if (!$tag->isSelfClosing()) {
    var_dump(htmlspecialchars($tag->close()));
    // string(10) ""
}
```

NullTag
-------

[](#nulltag)

The NullTag::class does not render any html tag at all only its html.

```
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"
```

Taggable
--------

[](#taggable)

```
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)
```

Attributes
----------

[](#attributes)

### Create Attributes

[](#create-attributes)

```
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:
    'required' => null,
    1 => 'readonly',

    // turns into json string:
    'data-bar' => ['key' => 'value'],
]);

var_dump((string)$attributes);
// string(90) " class="name" data-foo="" required readonly data-bar='{"key":"value"}'"
```

### Attributes Interface

[](#attributes-interface)

**empty**

```
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)
```

**has**

```
use Tobento\Service\Tag\Attributes;

$attributes = new Attributes(['class' => 'foo']);

var_dump($attributes->has('id'));
// bool(false)

var_dump($attributes->has('class'));
// bool(true)
```

**get**

```
use Tobento\Service\Tag\Attributes;

$attributes = new Attributes([
    'class' => 'foo',
    'data-foo' => ['key' => 'value'],
]);

var_dump($attributes->get(name: 'id'));
// NULL

var_dump($attributes->get('class'));
// string(3) "foo"

var_dump($attributes->get('data-foo'));
// array(1) { ["key"]=> string(5) "value" }
```

**set**

The set method does overwrite existing attributes.

```
use Tobento\Service\Tag\Attributes;

$attributes = new Attributes();

$attributes->set(name: 'class', value: 'foo');

$attributes->set('class', 'bar');

var_dump((string)$attributes);
// string(12) " class="bar""
```

You might just set the name only for certain attributes:

```
use Tobento\Service\Tag\Attributes;

$attributes = new Attributes();

$attributes->set('readonly');

var_dump((string)$attributes);
// string(9) " readonly"
```

**add**

The add method does "merge" existing attributes.

```
use Tobento\Service\Tag\Attributes;

$attributes = new Attributes();

$attributes->add(name: 'class', value: 'foo');

$attributes->add('class', 'bar');

var_dump((string)$attributes);
// string(16) " class="foo bar""

$attributes = new Attributes();

$attributes->add('data-cars', ['volvo' => 'Volvo']);
$attributes->add('data-cars', ['bmw' => 'BMW']);

var_dump((string)$attributes);
// string(82) " data-cars='{"volvo":"Volvo","bmw":"BMW"}'"
```

**merge**

The merge method merges the specified attributes with the existing attributes.

```
use Tobento\Service\Tag\Attributes;

$attributes = new Attributes([
    'class' => 'foo',
    'data-colors' => ['blue' => 'Blue'],
]);

$attributes->merge(attributes: [
    'class' => 'bar',
    'data-colors' => ['red' => 'Red'],
]);

var_dump((string)$attributes);
// string(98) " class="foo bar" data-colors='{"blue":"Blue","red":"Red"}'"
```

**all**

Returns all attributes.

```
use Tobento\Service\Tag\Attributes;

$attributes = new Attributes([
    'class' => 'foo',
]);

var_dump($attributes->all());
// array(1) { ["class"]=> string(3) "foo" }
```

**render**

```
use Tobento\Service\Tag\Attributes;

$attributes = new Attributes([
    'class' => 'foo',
]);

var_dump($attributes->render());
// string(12) " class="foo""
```

If there are attributes, it returns an empty space at the beginning, otherwise an empy string.

This way you can simply do the following:

```
>Lorem ipsum
```

**renderWithoutSpace**

```
use Tobento\Service\Tag\Attributes;

$attributes = new Attributes([
    'class' => 'foo',
]);

var_dump($attributes->renderWithoutSpace());
// string(11) "class="foo""
```

Str
---

[](#str)

**esc**

Returns the escaped string.

```
use Tobento\Service\Tag\Str;

$escapedString = Str::esc(
    string: 'string', // string|Stringable
    flags: ENT_QUOTES, // default
    encoding: 'UTF-8', // default
    double_encode: true // default
);

var_dump($escapedString);
// string(6) "string"
```

**formatTagAttributes**

Returns the formatted and escaped attributes as string.

```
use Tobento\Service\Tag\Str;

$string = Str::formatTagAttributes(
    attributes: [
        'class' => 'bar',
        'data-colors' => ['red' => 'Red'],
    ],
    withSpace: true, // default
);

var_dump($string);
// string(60) " class="bar" data-colors='{"red":"Red"}'"
```

Credits
=======

[](#credits)

- [Tobias Strub](https://www.tobento.ch)
- [All Contributors](../../contributors)

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance62

Regular maintenance activity

Popularity15

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~134 days

Recently: every ~265 days

Total

9

Last Release

238d ago

Major Versions

1.x-dev → 2.02025-09-23

PHP version history (2 changes)1.0.0PHP &gt;=8.0

2.0PHP &gt;=8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/055d6a1b5c2384bb179c75ab0b55914231d898fdc4dffeb30770f81200e52206?d=identicon)[TOBENTOch](/maintainers/TOBENTOch)

---

Top Contributors

[![tobento-ch](https://avatars.githubusercontent.com/u/16684832?v=4)](https://github.com/tobento-ch "tobento-ch (11 commits)")

---

Tags

phppackagehtmltagtobento

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tobento-service-tag/health.svg)

```
[![Health](https://phpackages.com/badges/tobento-service-tag/health.svg)](https://phpackages.com/packages/tobento-service-tag)
```

###  Alternatives

[okipa/laravel-table

Generate tables from Eloquent models.

56752.8k](/packages/okipa-laravel-table)[okipa/laravel-form-components

Ready-to-use and customizable form components.

198.0k1](/packages/okipa-laravel-form-components)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
