webfiori / php-structs
A library that provides basic utilities for creating HTML documents.
Fund package maintenance!
paypal.me/IbrahimBinAlshikh
Installs: 4 846
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 3
Forks: 0
Open Issues: 1
Requires
- php: >=7.0
- webfiori/collections: 1.1.x
Requires (Dev)
- phpunit/phpunit: ^5
- dev-master
- v2.6.3
- v2.6.2
- v2.6.1
- v2.5.6
- v2.5.5
- v2.5.4
- v2.5.3
- v2.5.2
- v2.5.1
- v2.5.0
- v2.4.0
- v2.3.5
- v2.3.4
- v2.3.3
- v2.3.2
- v2.3.1
- v2.3.0
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.0
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.9.7
- v1.9.6
- v1.9.5
- v1.9.4
- v1.9.3
- v1.9.2
- v1.9.1
- v1.9.0
- v1.8.9
- v1.8.8
- v1.8.7
- v1.8.6
- v1.8.5
- v1.8.4
- v1.8.3
- v1.8.2
- v1.8.1
- dev-dev
- dev-github-actions
- dev-bug-fix
This package is auto-updated.
Last update: 2024-07-08 22:04:52 UTC
README
A set of classes that provide basic web pages creation utilities in addition to creating the DOM of web pages.
Content
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:
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.
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.