A PHP library for creating HTML documents and DOM manipulation with an object-oriented approach.
PHP's built-in DOMDocument works but has friction for HTML generation:
- Verbose — Creating a styled element with text takes 4-5 lines. This library does it in one:
new HTMLNode('div', ['class' => 'x'])->text('hello') - No chaining —
DOMDocumentreturns void on most mutations. This library is fluent. - HTML5 issues —
DOMDocumentis XML-based (libxml2), throws warnings on HTML5 tags, and mishandles void elements - No templates — No built-in support for loading partial HTML with variable slots
- Limited output control —
saveHTML()gives little control over formatting, quoting, or self-closing tags
This library's sweet spot is generating HTML from PHP — pages, emails, components — where a builder pattern and template system give better DX than the W3C DOM API.
This is a DOM builder, not a text-based template engine. If you need template inheritance, compiled templates, or a dedicated syntax (like Blade or Twig), use those instead. Use this library when you want to construct and manipulate HTML programmatically — server-rendered components, email generation, PDF markup, or any case where the structure is driven by logic rather than a layout file.
- Key Features
- Supported PHP Versions
- Installation
- Quick Start
- Usage
- API Reference
- Testing
- Contributing
- License
- Support
- Changelog
- Object-Oriented DOM Creation — Build HTML elements using intuitive PHP classes
- Template System — Support for both HTML templates with slots and PHP templates
- Iterator Support — Traverse child nodes using foreach loops
- Type Safety — Full type hints and comprehensive PHPDoc documentation
- Security First — Built-in HTML entity escaping in attribute values
- XML Support — Generate both HTML and XML documents
- Standalone Renderer —
HtmlRendererwith per-instance config, safe for async contexts
| Build Status |
|---|
composer require webfiori/ui<?php
require_once 'vendor/autoload.php';
use WebFiori\Ui\HTMLDoc;
$doc = new HTMLDoc();
$doc->getHeadNode()->setPageTitle('My First Page');
$doc->setLanguage('en');
$body = $doc->getBody();
$body->addChild('h1')->text('Welcome to WebFiori UI!');
$body->addChild('p')->text('Building HTML has never been easier.');
echo $doc;use WebFiori\Ui\HTMLDoc;
$doc = new HTMLDoc();
$doc->getHeadNode()->setPageTitle('My Application');
$doc->setLanguage('en');
$head = $doc->getHeadNode();
$head->addMeta('description', 'A powerful web application');
$head->addCSS('styles/main.css');
$head->addJs('scripts/app.js');
$body = $doc->getBody();
$body->addChild('header')->addChild('h1')->text('My Application');
$body->addChild('main')->addChild('p')->text('Main content here.');
$body->addChild('footer')->addChild('p')->text('© 2024 My App');
echo $doc;use WebFiori\Ui\HTMLNode;
// Create elements with attributes
$div = new HTMLNode('div', ['id' => 'main', 'class' => 'container']);
$div->addChild('p')->text('Hello World');
// Method chaining
$div->setAttribute('data-role', 'content')
->setStyle(['padding' => '1rem']);
// Iterate children
foreach ($div as $child) {
echo $child->getNodeName();
}$form = $body->form(['method' => 'post', 'action' => '/login']);
$form->label('Username:');
$form->br();
$form->input('text', ['name' => 'username', 'required' => '']);
$form->br();
$form->label('Password:');
$form->br();
$form->input('password', ['name' => 'password', 'required' => '']);
$form->br();
$form->input('submit', ['value' => 'Login']);use WebFiori\Ui\HTMLTable;
$table = new HTMLTable(3, 4);
$table->getCell(0, 0)->text('Name');
$table->getCell(0, 1)->text('Email');HTML templates with slots:
<!-- template.html -->
<div class="card">
<h2>{{title}}</h2>
<p>{{content}}</p>
</div>$card = HTMLNode::fromFileAsNode('template.html', [
'title' => 'My Card',
'content' => 'Card body text'
]);PHP templates with variables:
// template.php
<ul>
<?php foreach ($items as $item): ?>
<li><?= htmlspecialchars($item) ?></li>
<?php endforeach; ?>
</ul>$list = HTMLNode::fromFileAsNode('template.php', [
'items' => ['Apple', 'Banana', 'Cherry']
]);| Class | Description |
|---|---|
HTMLNode |
Foundation class for all HTML elements |
HTMLDoc |
Represents a complete HTML document |
HeadNode |
The HTML head section |
HTMLTable |
Table creation and manipulation |
HtmlRenderer |
Standalone renderer with per-instance config |
TemplateCompiler |
HTML/PHP template loading and compilation |
| Method | Description |
|---|---|
addChild($node, $attrs) |
Add a child element |
setAttribute($name, $val) |
Set an attribute |
text($text) |
Set text content |
toHTML($formatted) |
Render to HTML string |
toXML($formatted) |
Render to XML string |
fromFile($path, $vars) |
Load from template |
fromFileAsDocument($path, $vars) |
Load as HTMLDoc |
fromFileAsNode($path, $vars) |
Load as single node |
fromFileAsArray($path, $vars) |
Load as node array |
cd tests
php ../vendor/bin/phpunitWe welcome contributions! Please see our Contributing Guide for details.
- Clone the repository
- Install dependencies:
composer install - Run tests:
cd tests && php ../vendor/bin/phpunit - Check code style:
composer fix-cs
This library is licensed under the MIT License. See the LICENSE file for details.
If you encounter any issues, please open an issue on GitHub.
See Releases for version history.