wongyip / html-tags
HTML Tags Renderer
Requires
- php: ^8.2
- wongyip/phphelpers: *
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
- dev-master
- v1.12.1
- v1.12.0
- v1.11.2
- v1.11.1
- v1.11.0
- v1.10.6
- v1.10.5
- v1.10.4
- v1.10.3
- v1.10.2
- v1.10.1
- v1.10.0
- v1.9.0
- v1.8.5
- v1.8.4
- v1.8.3
- v1.8.2
- v1.8.1
- v1.8.0
- v1.7.10
- v1.7.9
- v1.7.8
- v1.7.7
- v1.7.6
- v1.7.5
- v1.7.4
- v1.7.3
- v1.7.2
- v1.7.1
- v1.7.0
- v1.6.3
- v1.6.2
- v1.6.1
- v1.6.0
- v1.5.7
- v1.5.6
- v1.5.5
- v1.5.4
- v1.5.3
- v1.5.2
- v1.5.1
- v1.5.0
- v1.4.9
- v1.4.8
- v1.4.7
- v1.4.6
- v1.4.5
- v1.4.4
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.3
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.11
- v1.2.10
- v1.2.9
- v1.2.8
- v1.2.7
- v1.2.6
- v1.2.5
- v1.2.4
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.1
- v1.1.0
- v1.0.11
- v1.0.10
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
This package is auto-updated.
Last update: 2026-09-04 05:32:41 UTC
README
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
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.
The World is Dangerous, Always Sanitize Generated HTML.
Installation
composer require wongyip/html-tags
Usage
Basic
$div = Tag::make('#div1.some-css-class'); $div->style('font-size: 2em;'); $div->contents('Example <div> tag with class & style attributes.'); echo $div->render();
<div id="div1" class="some-css-class" style="font-size: 2em;">Example <div> tag with class & style attributes.</div>
The above output are not syntax highlighted to properly display the contents escaped by the htmlspecialchars function.
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()]);
<a href="/go/1" target="_blank">Go 1</a> <a href="/go/2" target="_blank">Go 2</a> <a href="/go/3" target="_blank">Go 3</a>
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();
<div class="parent"><p id="child1">Regular</p><p id="child2"><span><strong>Bold Face</strong></span></p></div>
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
For tags with conditional inner contents, extend the following protected methods — each returns a
ContentsCollection rendered at the corresponding position inside the tag:
TagAbstract::contentsBefore()- rendered right before the tag's contents.TagAbstract::contentsAfter()- rendered right after the tag's contents.
Contents Override
Alternatively, a tag may implement the ContentsOverride interface and
implement its contentsOverride() method to return a ContentsCollection that replaces the tag's main
contents at render time.
For example, the built-in Table tag implements ContentsOverride to compose its $caption,
$head and $body properties into its rendered contents. Other built-in tags using this mechanism include
Select, TBody and TR. See the contentsOverride demo
in Demo for a working Select example.
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();
<div>Sibling Before 1</div> <div>Sibling Before N</div> <div> <h1>Prefixed 1</h1> <h2>Prefixed N</h2> <p>Content 1</p> <p>Content N</p> <h3>Suffixed 1</h3> <h4>Suffixed N</h4> </div> <div>Sibling After 1</div> <div>Sibling After N</div>
Output above is hand-formatted with line breaks for readability — tags are actually rendered into a single line (see Limitations).
Comment
echo Comment::tag() ->contents('Comment ignores attributes set.') ->class('ignored') ->contentsAppend(Tag::make('div')->contents('Nested tag is fine.')) ->contentsAppend(Comment::tag()->contents('Nested comment ending brace is escaped.')) ->render();
<!-- Comment ignores attributes set.<div>Nested tag is fine.</div><!-- Nested comment ending brace is escaped. --> -->
The above output are not syntax highlighted to properly display the contents escaped by the htmlspecialchars function.
More Examples
- See
Demo::classfor more examples. - Run
php demo/demo.phpfor demonstration in CLI.
Limitations
- This is NOT a rendering engine.
<script>tag is not supported, obviously because of the security concerns.<style>tag is neither supported ashtmlspecialchars()might unintentionally break the styles.<!doctype>tag is not supported also.- Web content accessibility is not addressed, yet.
- Tags are rendered into a single line, if formatted HTML is needed, give HTML Beautify a try.
Always Sanitize Generated HTML.