Download the PHP package webfiori/php-structs without Composer
On this page you can find all versions of the php package webfiori/php-structs. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package php-structs
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
- Features
- Usage
- Basic Usage
- Building More Complex DOM
- HTML/PHP Template Files
- HTML Templates
- PHP Templates
- Creating XML Documents
- 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:
The output of this code is HTML 5 document. The structure of the document will be similar to the following HTML code:
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.
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:
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.
The output of the above PHP code will be the following HTML code.
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:
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:
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()
.
License
The library is licensed under MIT license.