8fold / php-html-builder
A library for building HTML document and element strings.
Installs: 1 925
Dependents: 2
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- 8fold/php-xml-builder: ^2.0
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.0
- squizlabs/php_codesniffer: ^3.7
README
HTML Builder extends and is a web-specific implementation of 8fold XML Builder; similar to how HTML extends and is a specific implementation of XML.
Installation
composer require 8fold/php-html-builder
Usage (basic)
Warning: Users of this library are responsible for sanitizing content.
By default, HTML Builder can quickly output an HTML string with consistent
ordering of attributes for elements. The default order can be found in the
Element
class, which can be overridden, if you prefer a different ordering. This helps
create and maintain a consistent HTML pattern. Further, this pattern can be
changed without updating templates or other code.
use Eightfold\HTMLBuilder\Document; use Eightfold\HTMLBuilder\Element; Document::create('title') ->head( Element::link() ->omitEndTag()->props('rel stylesheet', 'href style.css'), Element::script()->props('src script.js') )->body( Element::p('paragraph content') )
Output:
<!doctype html> <html lang="en"><head><title>title</title><meta charset="utf-8"><link rel="stylesheet" href="style.css"><script src="script.js"></script></head><body><p>paragraph content</p></body></html>
Output (formatted):
<!doctype html> <html lang="en"> <head> <title>title</title> <meta charset="utf-8"> <link rel="stylesheet" href="style.css"> <script src="script.js"></script> </head> <body> <p>paragraph content</p> </body> </html>
Further examples can be found in the
tests
folder with
an example extension.
A note on properties: If a property is not in the ordered list, and the value of the property is the same as the name of the property, HTML Builder will presume it's a boolean value and will truncate the definition in the HTML output.
Element::input()->omitEndTag()->props("required required");
Output:
<input required>
Performance
The /comparisons
folder can be used to run performance tests locally to compare the same output using:
- straight HTML,
- standard PHP,
- PHP HTML Builder alongside standard PHP (the Element class),
- PHP HTML Builder only (the Document class), and
- PHP HTML Builder only using PSR-4 autoloading.
These results are from February 26th, 2024:
Things worth noting (as rough interpretations):
- PHP being used as a template engine has a negligible impact on speed.
- Using
require_once
without PSR-4 autoloading decreases speed by roughly 1.5 milliseconds; however, once the code is made available, very little additional cost seems to be present. - Using PSR-4 decreases speed by roughly 6 milliseconds; however, ensures everything is present and available.
- Using PHP HTML Builder Document and Element resulted in 1 kilobyte reduction in packet size. (Increased processing time in milliseconds most likely offset by decreased download time.)
Details
See 8fold XML Builder for more details.