PHPackages                             webfiori/ui - 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. webfiori/ui

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

webfiori/ui
===========

A library that provides basic utilities for creating HTML documents.

4.0.0(7mo ago)542.4k—0%[1 issues](https://github.com/WebFiori/ui/issues)3MITPHPPHP &gt;=8.1CI passing

Since Apr 25Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/WebFiori/ui)[ Packagist](https://packagist.org/packages/webfiori/ui)[ RSS](/packages/webfiori-ui/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (55)Used By (3)

WebFiori UI Package
===================

[](#webfiori-ui-package)

 [![PHP Version](https://camo.githubusercontent.com/83dd395020c37276225039739320f6c8e7e99963ab21ee3d09282cb48dad2a60/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e312532422d626c7565)](https://camo.githubusercontent.com/83dd395020c37276225039739320f6c8e7e99963ab21ee3d09282cb48dad2a60/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e312532422d626c7565) [![Latest Version](https://camo.githubusercontent.com/7018945a7b82de2fd94ae4b8249303c5e6f2756739ad426ce197ad93b54b56a6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f77656266696f72692f7569)](https://camo.githubusercontent.com/7018945a7b82de2fd94ae4b8249303c5e6f2756739ad426ce197ad93b54b56a6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f77656266696f72692f7569) [![Total Downloads](https://camo.githubusercontent.com/347474407c395a5892702e980241ce60b05bced77d68d4a82140553852b1d4a3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f77656266696f72692f7569)](https://camo.githubusercontent.com/347474407c395a5892702e980241ce60b05bced77d68d4a82140553852b1d4a3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f77656266696f72692f7569) [![License](https://camo.githubusercontent.com/03925e66756362d95abbc866b2d2d6d974bce95f88113961c85c46c98012a5f7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f57656246696f72692f7569)](https://camo.githubusercontent.com/03925e66756362d95abbc866b2d2d6d974bce95f88113961c85c46c98012a5f7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f57656246696f72692f7569)

 [ ![](https://github.com/WebFiori/ui/actions/workflows/php84.yaml/badge.svg?branch=main) ](https://github.com/WebFiori/ui/actions) [ ![](https://camo.githubusercontent.com/7ee2c302e955830b2c7dd95a75cfd521f0391890a5f6163cc99197dd19b8bebc/68747470733a2f2f636f6465636f762e696f2f67682f57656246696f72692f75692f6272616e63682f6d61696e2f67726170682f62616467652e737667) ](https://codecov.io/gh/WebFiori/ui) [ ![](https://camo.githubusercontent.com/55ca623fecab72b811fd1335f39709a83ba849fe1d48e7b7bad1e7fd9ef17d90/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d57656246696f72695f7569266d65747269633d616c6572745f737461747573) ](https://sonarcloud.io/dashboard?id=WebFiori_ui)

A PHP library for creating HTML documents and DOM manipulation with an object-oriented approach. Build dynamic web pages, forms, tables, and complex UI components programmatically with clean, readable code.

✨ Features
----------

[](#-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
- 🌐 **XML Support** - Generate both HTML and XML documents

📋 Table of Contents
-------------------

[](#-table-of-contents)

- [Installation](#-installation)
- [Quick Start](#-quick-start)
- [Core Concepts](#-core-concepts)
- [HTML Document Creation](#-html-document-creation)
- [Working with Elements](#-working-with-elements)
- [Forms and Input](#-forms-and-input)
- [Tables and Data](#-tables-and-data)
- [Lists and Navigation](#-lists-and-navigation)
- [Images and Media](#-images-and-media)
- [Template System](#-template-system)
- [Styling and CSS](#-styling-and-css)
- [Advanced Features](#-advanced-features)
- [Performance Tips](#-performance-tips)
- [API Reference](#-api-reference)
- [Examples](#-examples)
- [Contributing](#-contributing)

🚀 Installation
--------------

[](#-installation)

The basic use case is to have HTML document with some text in its body. The class `HTMLDoc` represent HTML document. Simply, create an instance of this class and use it to build the whole HTML document. The class can be used as follows:

```
use WebFiori\Ui\HTMLDoc;

```bash
composer require webfiori/ui
```

Or add to your `composer.json`:

All HTML elements are represented as an instance of the class `HTMLNode`. Developers can extend this class to create custom UI components as classes. The library has already pre-made components which are used in the next code sample. In addition to that, the class has methods which utilize theses components and fasten the process of adding them as children of any HTML element. The following code shows a code which is used to create a basic login form.

```
use WebFiori\Ui\HTMLDoc;

//Create new instance of "HTMLDoc".
$doc = new HTMLDoc();

// Build a login form.
$body = $doc->getBody();
$body->text('Login to System')->hr();

$form = $body->form(['method' => 'post', 'actiion' => 'https://example.com/login']);

$form->label('Username:');
$form->br();
$form->input('text', ['placeholder'=>'You can use your email address.', 'style' => 'width:250px']);
$form->br();
$form->label('Password:');
$form->br();
$form->input('password', ['placeholder' => 'Type in your password here.', 'style' => 'width:250px']);
$form->br();
$form->input('submit', ['value' => 'Login']);

echo $doc;
```

The output of the code would be similar to the following image.

[![](tests/images/login-form.png)](tests/images/login-form.png)

### HTML/PHP Template Files

[](#htmlphp-template-files)

Some developers don't like to have everything in PHP. For example, front-end developers like to work directly with HTML since it has femiliar syntax. For this reason, the library include basic support for using HTML or PHP files as templates. If the templates are pure HTML, then variables are set in the document using slots. If the template has a mix between PHP and HTML, then PHP variables can be passed to the template.

#### HTML Templates

[](#html-templates)

Assume that we have HTML file with the following markup:

```
>

        {{page-title}}

            {{page-title}}

                Hello Mr.{{ mr-name }}. This is your visit number {{visit-number}}
                to our website.

```

It is noted that there are strings which are enclosed between `{{}}`. Any string enclosed between `{{}}` is called a slot. To fill any slot, its value must be passed when rendered in PHP. The file will be rendered into an instance of the class `HTMLNode`. The file can be rendered using the static method `HTMLNode::fromFile(string $templatePath, array $values)`. First parameter of the method is the path to the template and the second parameter is an associative array that holds values of slots. The keys of the array are slots names and the value of each index is the value of the slot. The following code shows how this document is loaded into an instance of the class `HTMLNode` with slots values.

```
$document = HTMLNode::fromFile('my-html-file.html', [
    'page-title' => 'Hello Page',
    'page-desc' => 'A page that shows visits numbers.',
    'mr-name' => 'Ibrahim Ali',
    'visit-number' => 33,
]);
echo $document
```

The output of the above PHP code will be the following HTML code.

```
>

        Hello Page

            Hello Page

                Hello Mr.Ibrahim Ali. This is your visit number 33
                to our website.

```

#### PHP Templates

[](#php-templates)

One draw back of using raw HTML template files with slots is that it can't have dynamic PHP code. To overcome this, it is possible to have the template written as a mix between HTML and PHP. This feature allow the use of all PHP features in HTML template. Also, this allow developers to pass PHP variables in addition to values for slots.

Assuming that we have the following PHP template that shows a list of posts titles:

```
