wongyip / html-tags
HTML Tags Renderer
Requires
- php: >=8.1
- ext-ctype: *
- ext-mbstring: *
- dev-master
- 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: 2025-04-10 06:34:57 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 = new Tag('div'); $div->id('some-css-class') $div->style('font-size: 2em;') $div->contents('Example <div> tag with class & style attributes.'); echo $div->render();
<div id="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 scenario of tags with conditional inner contents, you may extend the following methods to handle the needs:
TagAbstract::contentsBefore()
, that output aContentsCollection
for render right before the tag's contents.TagAbstract::contentsAfter()
that output aContentsCollection
for render right after the tag's contents.
For example, the build-in Table
tag extended the contentsBefore()
method to insert its
caption
, thead
and tbody
when they're set.
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>
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();
<!-- 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::class
for more examples. - Run
php demo/demo.php
for 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.