PHPackages                             wongyip/html-tags - 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. wongyip/html-tags

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

wongyip/html-tags
=================

HTML Tags Renderer

v1.10.3(9mo ago)0817↓75%2MITPHPPHP ^8.2

Since Apr 1Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/wongyip/html-tags)[ Packagist](https://packagist.org/packages/wongyip/html-tags)[ Docs](https://github.com/wongyip/html-tags)[ RSS](/packages/wongyip-html-tags/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (75)Used By (2)

HTML Tags Renderer
==================

[](#html-tags-renderer)

A simple HTML renderer with fluent interface to generate dynamic components. Those generated tags are self-rendered, so it is not necessary to build extra logic into templates to handle them, which helps to reduce the complexity of templates and also make certain components independent of template engines.

Read This First
---------------

[](#read-this-first)

This package doesn't take cares of security at all, if your application generates HTML from dynamic data, especially user-contributed contents, be very careful to avoid something like XSS attack. You should always employ your own favour of HTML filtering tool, e.g. [HTML Purifier](https://github.com/ezyang/htmlpurifier).

**The World is Dangerous, Always Sanitize Generated HTML.**

Installation
------------

[](#installation)

```
composer require wongyip/html-tags
```

Usage
-----

[](#usage)

### Basic

[](#basic)

```
$div = Tag::make('#div1.some-css-class');
$div->style('font-size: 2em;');
$div->contents('Example  tag with class & style attributes.');
echo $div->render();
```

```
Example &lt;div&gt; tag with class &amp; style attributes.

```

*The above output are not syntax highlighted to properly display the contents escaped by the `htmlspecialchars` function.*

### Various Coding Style

[](#various-coding-style)

Tags may be rendered in different ways to fit into different scenarios.

```
// Spell out everything if you care about who read your code.
$a1 = Anchor::tag()->href('/go/1')->target('_blank')->contents('Go 1');

// When working with structural data like a data model.
$a2 = Anchor::tag('Go 2')->attributes(['href' => '/go/2', 'target' => '_blank']);

// Code a little less with tailor-made creator-function.
$a3 = Anchor::create('/go/3', 'Go 3', '_blank');

echo implode(PHP_EOL, [$a1->render(), $a2->render(), $a3->render()]);
```

```
Go 1
Go 2
Go 3
```

### Nesting

[](#nesting)

```
$tag = Tag::make('div')->class('parent')->contents(
    Tag::make('p')->id('child1')->contents('Regular'),
    Tag::make('p')->id('child2')->contents(
        Tag::make('span')->contents(
            Tag::make('strong')->contents('Bold Face')
        )
    )
);
echo $tag->render();
```

```
RegularBold Face
```

### Contents Collections

[](#contents-collections)

Tags extending the `TagAbstract` will have a several `ContentsCollection` properties, which are empty on instantiate, and accessible in public scope. You may make use of them to manipulate all the contents of a tag.

- `TagAbstract::$siblingsBefore` - sibling tags with the same nesting level of the tag, rendered before the tag.
- `TagAbstract::$contentsPrefixed` - inner contents on top of the tag.
- `TagAbstract::$contents` - inner contents of the tag.
- `TagAbstract::$contentsSuffixed` - inner contents at the bottom of the tag.
- `TagAbstract::$siblingsAfter` - sibling tags with the same nesting level of the tag, rendered after the tag.

### Extensible Contents

[](#extensible-contents)

For scenario of tags with conditional inner contents, you may extend the following methods to handle the needs:

- `TagAbstract::contentsBefore()`, that output a `ContentsCollection` for render right before the tag's contents.
- `TagAbstract::contentsAfter()` that output a `ContentsCollection` for render right after the tag's contents.

For example, the build-in [`Table`](/src/Table.php) tag extended the `contentsBefore()` method to insert its `caption`, `thead` and `tbody` when they're set.

### Contents Anatomy, Concluded

[](#contents-anatomy-concluded)

- Siblings Before the Tag
    - Prefixed Contents
    - Extensible Contents (Before)
    - Contents
    - Extensible Contents (After)
    - Suffixed Contents
- Siblings After the Tag

The following example illustrates the above anatomy excepts of the Extensible Contents (Before and After):

```
$tag = Tag::make('div');
$tag->contents->append(Tag::make('p')->contents('Content 1'), Tag::make('p')->contents('Content N'));
$tag->contentsPrefixed->append(Tag::make('h1')->contents('Prefixed 1'), Tag::make('h2')->contents('Prefixed N'));
$tag->contentsSuffixed->append(Tag::make('h3')->contents('Suffixed 1'), Tag::make('h4')->contents('Suffixed N'));
$tag->siblingsBefore->append(Tag::make('div')->contents('Sibling Before 1'));
$tag->siblingsBefore->append(Tag::make('div')->contents('Sibling Before N'));
$tag->siblingsAfter->append(Tag::make('div')->contents('Sibling After 1'));
$tag->siblingsAfter->append(Tag::make('div')->contents('Sibling After N'));
echo $tag->render();
```

```
Sibling Before 1
Sibling Before N

    Prefixed 1
    Prefixed N
    Content 1
    Content N
    Suffixed 1
    Suffixed N

Sibling After 1
Sibling After N
```

### Comment

[](#comment)

```
echo Comment::make()
    ->contents('Comment ignores attributes set.')
    ->class('ignored')
    ->contentsAppend(Tag::make('div')->contents('Nested tag is fine.'))
    ->contentsAppend(Comment::make()->contents('Nested comment ending brace is escaped.'))
    ->render();
```

```

```

*The above output are not syntax highlighted to properly display the contents escaped by the `htmlspecialchars` function.*

### More Examples

[](#more-examples)

- See [`Demo::class`](src/Demo/Demo.php) for more examples.
- Run `php demo/demo.php` for demonstration in CLI.

Limitations
-----------

[](#limitations)

1. This is NOT a rendering engine.
2. `` tag is not supported, obviously because of the security concerns.
3. `` tag is neither supported as `htmlspecialchars()` might unintentionally break the styles.
4. `` tag is not supported also.
5. Web content accessibility is not addressed, yet.
6. Tags are rendered into a single line, if formatted HTML is needed, give [HTML Beautify](https://github.com/wongyip/html-beautify) a try.

---

**Always Sanitize Generated HTML.**

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance58

Moderate activity, may be stable

Popularity17

Limited adoption so far

Community12

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 ~7 days

Recently: every ~24 days

Total

74

Last Release

273d ago

PHP version history (2 changes)v1.0.0PHP &gt;=8.1

v1.10.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/d49e948c3119a3b6a39247b96f577d4df2628b9a15583ef0fbf128b0b41d8622?d=identicon)[wongyip](/maintainers/wongyip)

---

Top Contributors

[![wongyip](https://avatars.githubusercontent.com/u/9350540?v=4)](https://github.com/wongyip "wongyip (74 commits)")

### Embed Badge

![Health badge](/badges/wongyip-html-tags/health.svg)

```
[![Health](https://phpackages.com/badges/wongyip-html-tags/health.svg)](https://phpackages.com/packages/wongyip-html-tags)
```

###  Alternatives

[tecnocen/yii2-bootstrap-year-calendar

Yii2 widget for bootstrap-year-calendar plugin

1317.7k](/packages/tecnocen-yii2-bootstrap-year-calendar)[rspeekenbrink/laravel-menu

Simple menu generation in Laravel

164.3k](/packages/rspeekenbrink-laravel-menu)

PHPackages © 2026

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