8fold/php-html-builder

A library for building HTML document and element strings.

Fund package maintenance!
joshbruce
8fold

2.4.0 2023-04-30 18:51 UTC

This package is auto-updated.

Last update: 2024-10-27 01:29:07 UTC


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:

  1. straight HTML,
  2. standard PHP,
  3. PHP HTML Builder alongside standard PHP (the Element class),
  4. PHP HTML Builder only (the Document class), and
  5. PHP HTML Builder only using PSR-4 autoloading.

These results are from February 26th, 2024:

Things worth noting (as rough interpretations):

  1. PHP being used as a template engine has a negligible impact on speed.
  2. 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.
  3. Using PSR-4 decreases speed by roughly 6 milliseconds; however, ensures everything is present and available.
  4. 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.

Other