webfiori / ui
A library that provides basic utilities for creating HTML documents.
Requires
- php: >=8.1
- webfiori/collections: 2.0.x
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.86
- phpunit/phpunit: ^10
This package is auto-updated.
Last update: 2026-06-10 15:51:29 UTC
README
A PHP library for creating HTML documents and DOM manipulation with an object-oriented approach.
Motivations
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.
Table of Contents
- Key Features
- Supported PHP Versions
- Installation
- Quick Start
- Usage
- API Reference
- Testing
- Contributing
- License
- Support
- Changelog
Key Features
- 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
Supported PHP Versions
| Build Status |
|---|
Installation
composer require webfiori/ui
Quick Start
<?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;
Usage
HTML Document Creation
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;
Working with Elements
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(); }
Forms and Input
$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']);
Tables and Data
use WebFiori\Ui\HTMLTable; $table = new HTMLTable(3, 4); $table->getCell(0, 0)->text('Name'); $table->getCell(0, 1)->text('Email');
Template System
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'] ]);
API Reference
Core Classes
| 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 |
Key Methods (HTMLNode)
| 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 |
Testing
cd tests
php ../vendor/bin/phpunit
Contributing
We 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
License
This library is licensed under the MIT License. See the LICENSE file for details.
Support
If you encounter any issues, please open an issue on GitHub.
Changelog
See Releases for version history.