shish / microhtml
A minimal HTML generating library
v2.8.0
2026-03-13 15:50 UTC
Requires
- php: ^8.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.89
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
README
Moving from Hack to vanilla PHP, I miss XHP T__T
This isn't XHP, but it does provide a minimum-bullshit method of generating HTML in a consistent and secure manner
<?php use function MicroHTML\{HTML,SECTION,H1,P,DIV}; $page = HTML( SECTION(["id"=>"news"], H1("My title"), P("Here's some content") ) ); $page->appendChild( SECTION(["id"=>"comments"], DIV("Oh noes: <script>alert('a haxxor is attacking us');</script>") ) ); print($page);
<html> <section id='news'> <h1>My title</h1> <p>Here's some content</p> </section> <section id='comments'> <div>Oh noes: <script>alert('a haxxor is attacking us');</script></div> </section> </html>
Security Notes
- Tag attribute values and text content are escaped by default
- Tag names and attribute names are not escaped, it is assumed that the developer will be hard-coding these
- Tag attributes are not filtered other than escaping, so be careful when allowing user input in attributes (e.g.
href,src) to avoid XSS vulnerabilities rawHTML()can be used to insert unescaped content - it is the developer's responsibility to ensure that this content is safe
Build
src/microhtml.php is generated by gen.php
Testing
composer install
composer check
Release
git tag v1.2.3
git push --tags