A library that provides basic utilities for creating HTML documents.

Maintainers

Details

github.com/WebFiori/ui

Source

Issues

Fund package maintenance!
paypal.me/IbrahimBinAlshikh

v2.6.1 2023-12-03 18:05 UTC

README

A set of classes that provide basic web pages creation utilities in addition to creating the DOM of web pages.

badge.svg?branch=master 68747470733a2f2f636f6465636f762e696f2f67682f57656246696f72692f75692f6272616e63682f6d61737465722f67726170682f62616467652e737667 68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d57656246696f72695f7569266d65747269633d616c6572745f737461747573 68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f57656246696f72692f75692e7376673f6c6162656c3d6c6174657374 68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f77656266696f72692f75693f636f6c6f723d6c696768742d677265656e

Content

Supported PHP Versions

Build Status
badge.svg?branch=master
badge.svg?branch=master
badge.svg?branch=master
badge.svg?branch=master
badge.svg?branch=master
badge.svg?branch=master
badge.svg?branch=master
badge.svg?branch=master
badge.svg?branch=master

Features

  • Ability to create custom HTML UI Elements in OOP approach.
  • Virtual DOM through PHP.
  • Building dynamic HTML templates with PHP.
  • Support for rendering XML documents.

Usage

Basic Usage

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;

//This code will create HTML5 Document, get the <body> node and, add text to it.
$doc = new HTMLDoc();
$doc->getBody()->text('Hello World!');
echo $doc;

The output of this code is HTML 5 document. The structure of the document will be similar to the following HTML code:

<!DOCTYPE html>
<html>
  <head>
    <title>
      Default
    </title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
  </head>
  <body itemscope itemtype="http://schema.org/WebPage">
    Hello World!
  </body>
</html>

Building More Complex DOM

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.

login-form.png

HTML/PHP 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

Assume that we have HTML file with the following markup:

<!DOCTYPE html>
<html>
    <head>
        <title>{{page-title}}</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="{{page-desc}}">
    </head>
    <body>
        <section>
            <h1>{{page-title}}</h1>
            <p>
                Hello Mr.{{ mr-name }}. This is your visit number {{visit-number}} 
                to our website.
            </p>
        </section>
    </body>
</html>

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.

<!DOCTYPE html>
<html>
    <head>
        <title>Hello Page</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="A page that shows visits numbers.">
    </head>
    <body>
        <section>
            <h1>Hello Page</h1>
            <p>
                Hello Mr.Ibrahim Ali. This is your visit number 33
                to our website.
            </p>
        </section>
    </body>
</html>

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:

<div>
    <?php 
    if (count($posts) != 0) {?>
    <ul>
    <?php
        foreach ($posts as $postTitle) {?>
        <li><?= $postTitle;?></li>
        <?php
        }
        ?>
    </ul>
    <?php
    } else {
        echo "No posts.\n";
    }
    ?>
</div>

This template uses a variable called $posts as seen. The value of this variable must be passed to the template before rendering. In this case, the second parameter of the method HTMLNode::fromFile(string $templatePath, array $values) will have associative array of variables. The keys of the array are variables names and the values are variables values.

The template can be loaded into object of type HTMLNode as follows:

$posts = [
  'Post 1',
  'Post 2',
  'Post 3'
];

$node = HTMLNode::fromFile('posts-list.php', [
  'posts' => $posts
])

Creating XML Documents

In addition to representing HTML elements, the class HTMLNode can be used to represent XML document. The difference between HTML and XML is that XML is case-sensitive for attributes names and elements names in addition to not having a pre-defined elements like HTML. To create XML document, the class HTMLNode can be used same way as It's used in creating HTML elements. At the end, the element can be converted to XML by using the method HTMLNode::toXML().

$xml = new HTMLNode('saml:Assertion', [
   'xmlns:saml' => "urn:oasis:names:tc:SAML:2.0:assertion",
   'xmlns:xs' => "http://www.w3.org/2001/XMLSchema",
   'ID' => "_d71a3a8e9fcc45c9e9d248ef7049393fc8f04e5f75",
   'Version' => "2.0",
   'IssueInstant' => "2004-12-05T09:22:05Z",
]);
$xml->addChild('saml:Issuer')->text('https://idp.example.org/SAML2');

echo $xml->toXML();
//Output:
/*
<?xml version="1.0" encoding="UTF-8"?>
<saml:Assertion xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:xs="http://www.w3.org/2001/XMLSchema" ID="_d71a3a8e9fcc45c9e9d248ef7049393fc8f04e5f75" Version="2.0" IssueInstant="2004-12-05T09:22:05Z">
    <saml:Issuer>
        https://idp.example.org/SAML2
    </saml:Issuer>
</saml:Assertion>
*/

License

The library is licensed under MIT license.