PHPackages                             crell/htmlmodel - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. crell/htmlmodel

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

crell/htmlmodel
===============

Domain value objects for modeling HTML pages

1.0.0(9y ago)21282MITPHPPHP &gt;=5.5.0

Since Nov 27Pushed 7y ago2 watchersCompare

[ Source](https://github.com/Crell/HtmlModel)[ Packagist](https://packagist.org/packages/crell/htmlmodel)[ Docs](https://github.com/Crell/HtmlModel)[ RSS](/packages/crell-htmlmodel/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (4)Versions (3)Used By (0)

HtmlModel
=========

[](#htmlmodel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/e7ec4206a312a24f5d3aba9d0174a3319f0f82ff05a039c0631ec45dd06f059d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6372656c6c2f68746d6c6d6f64656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/crell/htmlmodel)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/031ccfab2083b4c0ea357bbe734e978cd692b8ea6787e136877d448a212281e4/68747470733a2f2f7472617669732d63692e6f72672f4372656c6c2f48746d6c4d6f64656c2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Crell/HtmlModel.svg?branch=master)[![Code Climate](https://camo.githubusercontent.com/9fcb960fb7d0b562c4acfd7b927e45c08133ba3b058b810d8b2077bf792bf8db/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f4372656c6c2f48746d6c4d6f64656c2f6261646765732f6770612e737667)](https://codeclimate.com/github/Crell/HtmlModel)[![Total Downloads](https://camo.githubusercontent.com/181d7196399d34c175a4ff42b6a558743630378f7fde719a06a0c48ba9738912/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6372656c6c2f68746d6c6d6f64656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/crell/htmlmodel)

HtmlModel is exactly what it sounds like. It is a series of value objects intended to model an HTML page. It does not attempt to model every element in HTML (that would be silly), but just the key aspects of it. In a sense, it seeks to provide an HTML equivalent of RESTful domain models such as HAL or Atom.

Inspired by [PSR-7](https://www.php-fig.org/psr/psr-7), all objects are immutable. They may be manipulated with with\*() methods, which return new value object instances. Link handling uses the [PSR-13](https://www.php-fig.org/psr/psr-13) hyperlink specification.

This approach was inspired by, and evolved from, similar code that exited in Drupal 8 during development but was later removed.

Install
-------

[](#install)

Via Composer

```
$ composer require crell/htmlmodel
```

Usage
-----

[](#usage)

If you've used a PSR-7 Request or Response object, HtmlModel should be quite similar. Most of the time you'll be interacting with either an HtmlFragment or an HtmlPage.

```
// Create an HtmlFragment object.
$fragment = new HtmlFragment();

// Set the fragment body, which is simply an arbitrary HTML string.
$fragment = $fragment->withContent('Immutable objects are easier than you think.');

// Populate its metadata; the metadata won't be rendered with this
// fragment but can be transferred to an aggregating page, or a JSON
// instruction in response to an Ajax call.
$fragment = $fragment
  ->withHeadElement(new MetaRefreshElement(3, 'http://www.google/com'))
  ->withHeadElement(new LinkElement('canonical', 'http://www.example.com/'))
  ->withStyleLink(new StyleLinkElement('css.css'))
;
```

```
// Create an HtmlPage object.
$html = new HtmlPage();

// Populate it with various elements and body contents.
$html = $html
  ->withTitle('Test page')
  ->withHtmlAttribute('manifest', 'example.appcache')
  ->withBodyAttribute('foo', 'bar')
  ->withBase(new BaseElement('http://www.example.com/'))
  ->withHeadElement(new MetaRefreshElement(3, 'http://www.google.com'))
  ->withHeadElement(new LinkElement('canonical', 'http://www.example.com/'))
  ->withScript(new ScriptElement('header.js'))
  ->withScript(new ScriptElement('footer.js'), 'footer')
  ->withStyleLink(new StyleLinkElement('css.css'))
  ->withInlineStyle(new StyleElement('CSS here'))
  ->withContent('Body here')
;

// Simply casting the page object to a string will produce the corresponding markup.
$output = (string)$html;
```

The HtmlPage can even contain an HTTP status code. Although it won't get rendered it can be transferred to a Response object, allowing the page creator to specify the type of response through a straightforward domain object.

The cool part, though, is when you can aggregate multiple fragments into a page cleanly. That lets you build multiple portions of a page in parallel, even asynchronously, even caching some portions of the page but not others, and then fold them together into a single page.

```
// Create an HtmlFragment (or return one from some lower-level routine)
$src = new HtmlFragment();
$src = $src
  ->withHeadElement(new MetaRefreshElement(3, 'http://www.google.com'))
  ->withHeadElement(new LinkElement('canonical', 'http://www.example.com/'))
  ->withScript(new ScriptElement('js.js'))
  ->withScript(new ScriptElement('footer.js'), 'footer')
  ->withScript($inline_script)
  ->withStyleLink(new StyleLinkElement('css.css'))
  ->withInlineStyle(new StyleElement('CSS here'))
  ->withContent('Body here')
;

// Now make an HtmlPage.
$dest = new HtmlPage();

// Now shuffle the metadata from the fragment to the page.

$transferer = new AggregateMetadataTransferer([
  StyleContainerInterface::class => new StyleTransferer(),
  ScriptContainerInterface::class => new ScriptTransferer(),
  StatusCodeContainerInterface::class => new StatusCodeTransferer(),
  HeadElementContainerInterface::class => new HeadElementTransferer(),
]);

$html = $transferer->transfer($src, $dest);

// Now take other fragments and transfer their metadata to the page, aggregating them together!
```

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

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

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email the author instead of using the issue tracker.

Credits
-------

[](#credits)

- [Larry Garfield](https://github.com/Crell)

License
-------

[](#license)

The LGPL License, version 3 or, at your option, any later version. Please see [License File](LICENSE.md) for more information.

###  Health Score

29

—

LowBetter than 58% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 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 ~0 days

Total

2

Last Release

3481d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/12e28c223b88445f07d697c8805bd856066c947f70b535f6a7e00d2cb311c3c2?d=identicon)[Crell](/maintainers/Crell)

---

Top Contributors

[![Crell](https://avatars.githubusercontent.com/u/254863?v=4)](https://github.com/Crell "Crell (112 commits)")

---

Tags

html

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/crell-htmlmodel/health.svg)

```
[![Health](https://phpackages.com/badges/crell-htmlmodel/health.svg)](https://phpackages.com/packages/crell-htmlmodel)
```

###  Alternatives

[spatie/laravel-html

A fluent html builder

8307.1M91](/packages/spatie-laravel-html)[ckeditor/ckeditor

JavaScript WYSIWYG web text editor.

5244.3M79](/packages/ckeditor-ckeditor)[caxy/php-htmldiff

A library for comparing two HTML files/snippets and highlighting the differences using simple HTML.

21421.8M20](/packages/caxy-php-htmldiff)[yajra/laravel-datatables-html

Laravel DataTables HTML builder plugin

28410.1M49](/packages/yajra-laravel-datatables-html)[wa72/htmlpagedom

jQuery-inspired DOM manipulation extension for Symfony's Crawler

3494.0M34](/packages/wa72-htmlpagedom)[tinymce/tinymce

Web based JavaScript HTML WYSIWYG editor control.

1697.9M118](/packages/tinymce-tinymce)

PHPackages © 2026

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