Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/webfiori/ui
A set of classes for creating HTML documents.
https://github.com/webfiori/ui
data-structures dom hacktoberfest html html-document html-element linked-list php slot webfiori-framework webfiori-ui
Last synced: about 2 months ago
JSON representation
A set of classes for creating HTML documents.
- Host: GitHub
- URL: https://github.com/webfiori/ui
- Owner: WebFiori
- License: mit
- Created: 2018-03-19T09:23:26.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-07-08T22:04:49.000Z (6 months ago)
- Last Synced: 2024-08-10T21:21:48.128Z (5 months ago)
- Topics: data-structures, dom, hacktoberfest, html, html-document, html-element, linked-list, php, slot, webfiori-framework, webfiori-ui
- Language: PHP
- Homepage: https://webfiori.com/learn/ui-package
- Size: 921 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# WebFiori UI Package
A set of classes that provide basic web pages creation utilities in addition to creating the DOM of web pages.## Content
* [Supported PHP Versions](#supported-php-versions)
* [Features](#features)
* [Usage](#usage)
* [Basic Usage](#basic-usage)
* [Building More Complex DOM](#building-more-complex-dom)
* [HTML/PHP Template Files](#htmlphp-template-files)
* [HTML Templates](#html-templates)
* [PHP Templates](#php-templates)
* [Creating XML Documents](#creating-xml-Documents)
* [License](#license)
## Supported PHP Versions
| Build Status |
|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|
| |
| |
| |
| |
| |
| |
| |
| |
| |## 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:
``` php
use webfiori\ui\HTMLDoc;//This code will create HTML5 Document, get the 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:
``` html
Default
Hello World!
```
### Building More Complex DOMAll 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.
``` php
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.
### 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:
``` html
{{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.
``` php
$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.
``` html
Hello Page
Hello Page
Hello Mr.Ibrahim Ali. This is your visit number 33
to our website.
```
#### PHP TemplatesOne 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:
``` php
- = $postTitle;?>
```
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:
``` php
$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()`.``` php
$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:
/*
https://idp.example.org/SAML2
*/
```## License
The library is licensed under MIT license.