PHPackages                             royallthefourth/html-document - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. royallthefourth/html-document

ActiveLibrary[Templating &amp; Views](/categories/templating)

royallthefourth/html-document
=============================

An object-oriented tool for building HTML documents

v1.0.7(9y ago)16281[1 issues](https://github.com/royallthefourth/html-document/issues)MITPHPPHP ^7

Since Nov 21Pushed 9y ago2 watchersCompare

[ Source](https://github.com/royallthefourth/html-document)[ Packagist](https://packagist.org/packages/royallthefourth/html-document)[ RSS](/packages/royallthefourth-html-document/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (8)Dependencies (1)Versions (10)Used By (0)

html-document
=============

[](#html-document)

[![SensioLabsInsight](https://camo.githubusercontent.com/13d0c5073e941479bb26181fc99baf0353a8c1630e820a3036d605b201d05cec/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f65653861306663322d306462382d343562322d383662392d3335666637653136356234632f6d696e692e706e67)](https://insight.sensiolabs.com/projects/ee8a0fc2-0db8-45b2-86b9-35ff7e165b4c)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/0ac543e0d85ab2f08a574b6cb4a301ca2bee2d7f4af666ebdf19b67f0eccef74/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f726f79616c6c746865666f757274682f68746d6c2d646f63756d656e742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/royallthefourth/html-document/?branch=master)

`html-document` is an object oriented tool for building HTML documents. It's intended to be more practical than the standard DOM implementation, but it's not exactly a by-the-book DOM implementation.

What does it do?
----------------

[](#what-does-it-do)

- Provides all standard HTML5 elements as classes
- Provides methods to compose these elements into a document hierarchy
- Renders a hierarchy of elements into a single string for output to the browser

What does it not do?
--------------------

[](#what-does-it-not-do)

- Provide any validation for the documents you create (for now)
- Prevent XSS or other injection-related attacks
- Read or manipulate already existing documents

Installation
------------

[](#installation)

Install with [Composer](https://getcomposer.org/). Add the library to your `composer.json`:

```
{
    "require": {
        "royallthefourth/html-document": "^1.0"
    }
}
```

Then install:

```
composer install --no-dev

```

This library has no dependencies.

Example
-------

[](#example)

Building all or part of an HTML document with this library is easy. Take a look:

```
use RoyallTheFourth\HtmlDocument\Document;
use RoyallTheFourth\HtmlDocument\Element\Body;
use RoyallTheFourth\HtmlDocument\Element\Head;
use RoyallTheFourth\HtmlDocument\Element\Html;
use RoyallTheFourth\HtmlDocument\Element\Paragraph;
use RoyallTheFourth\HtmlDocument\Element\Text;
use RoyallTheFourth\HtmlDocument\Element\Title;

echo (new Document())
    ->add((new Html())
        ->withChild((new Head())
            ->withChild((new Title())
                ->withChild(new Text('HTML Document'))
            )
        )
        ->withChild((new Body())
            ->withChild((new Paragraph())
                ->withChild(new Text('Build a whole document at once like this, or piece existing parts together'))
            )
        )
    )->render();
```

This returns a complete HTML page as a string:

```
>

HTML Document

Build a whole document at once like this, or piece existing parts together

```

Of course, you're unlikely to want to build whole documents in place like this. The real idea behind this library is to allow objects to represent themselves as elements to put on a page. For example, you might have an object that can take a `Layout` object to provide presentation behavior. Injecting a `TableRowLayout` object will provide the behaviors of the layout object without breaking encapsulation. Now your object can represent itself as a `` without the need for a templating engine, public properties, or getter methods. With this approach, a thoughtfully composed system of objects leads directly to a finished HTML document. See [my blog](https://royall.us/ditch-the-template-system/) for a more detailed example.

One important difference from the usual conception of HTML documents is that these elements do not have values. Instead, the library provides a special element type called `Text` that can be used to place arbitrary text within any node that has open and close tags.

There are also `Arbitrary` and `ArbitraryEmpty` elements in case you need a nonstandard element. The only difference between these two is that `Arbitrary` has a closing tag while `ArbitraryEmpty` does not. For example, `` could be implemented as an `ArbitraryEmpty`.

For more examples, see the `tests` directory. My intention is for this library to be very straightforward and self-documenting.

Safety
------

[](#safety)

This library **does not** sanitize its inputs at all. Treat everything you pass into this library as if it's getting echoed directly onto the page. [`htmlspecialchars()`](http://php.net/htmlspecialchars) should be enough to prevent any unwanted markup from appearing.

If I am mistaken about any of this, please notify me right away.

Contributing
------------

[](#contributing)

Bug reports, bug fixes, tests, and documentation are heartily welcomed. If something in the library is missing or out of date, I'd love for it to be brought up to standard. If it's a simple bugfix, go ahead and open a pull request. Please include a test that exposes the bug along with your bugfix.

For anything more complex, open an issue first so we can discuss how to handle it.

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~19 days

Recently: every ~33 days

Total

8

Last Release

3324d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1503994?v=4)[Royall Spence](/maintainers/royallthefourth)[@royallthefourth](https://github.com/royallthefourth)

---

Top Contributors

[![royallthefourth](https://avatars.githubusercontent.com/u/1503994?v=4)](https://github.com/royallthefourth "royallthefourth (81 commits)")

---

Tags

domhtml-generationphp7

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/royallthefourth-html-document/health.svg)

```
[![Health](https://phpackages.com/badges/royallthefourth-html-document/health.svg)](https://phpackages.com/packages/royallthefourth-html-document)
```

###  Alternatives

[mustache/mustache

A Mustache implementation in PHP.

3.3k44.6M291](/packages/mustache-mustache)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[whitecube/nova-flexible-content

Flexible Content &amp; Repeater Fields for Laravel Nova.

8053.0M25](/packages/whitecube-nova-flexible-content)[mopa/bootstrap-bundle

Easy integration of twitters bootstrap into symfony2

7042.9M33](/packages/mopa-bootstrap-bundle)[limenius/react-bundle

Client and Server-side react rendering in a Symfony Bundle

3871.2M](/packages/limenius-react-bundle)[nicmart/string-template

StringTemplate is a very simple string template engine for php. I've written it to have a thing like sprintf, but with named and nested substutions.

2101.7M30](/packages/nicmart-string-template)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
