Skip to content

Commit a833f98

Browse files
authored
chore: Merge pull request #49 from WebFiori/dev
Release-As: v4.0.0
2 parents a69a2ce + 0863716 commit a833f98

37 files changed

+195
-90
lines changed

.github/workflows/php83.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ jobs:
3232
name: Code Quality
3333
needs: test
3434
uses: WebFiori/workflows/.github/workflows/quality-sonarcloud.yaml@main
35-
with:
36-
coverage-file: 'php-8.3-coverage.xml'
3735
secrets:
3836
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
3937

README.md

Lines changed: 126 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,125 @@ A PHP library for creating HTML documents and DOM manipulation with an object-or
5151

5252
## 🚀 Installation
5353

54-
Install via Composer:
54+
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:
55+
``` php
56+
use WebFiori\Ui\HTMLDoc;
5557

5658
```bash
5759
composer require webfiori/ui
5860
```
5961

6062
Or add to your `composer.json`:
6163

62-
```json
63-
{
64-
"require": {
65-
"webfiori/ui": "^3.0"
64+
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.
65+
66+
``` php
67+
use WebFiori\Ui\HTMLDoc;
68+
69+
//Create new instance of "HTMLDoc".
70+
$doc = new HTMLDoc();
71+
72+
// Build a login form.
73+
$body = $doc->getBody();
74+
$body->text('Login to System')->hr();
75+
76+
$form = $body->form(['method' => 'post', 'actiion' => 'https://example.com/login']);
77+
78+
$form->label('Username:');
79+
$form->br();
80+
$form->input('text', ['placeholder'=>'You can use your email address.', 'style' => 'width:250px']);
81+
$form->br();
82+
$form->label('Password:');
83+
$form->br();
84+
$form->input('password', ['placeholder' => 'Type in your password here.', 'style' => 'width:250px']);
85+
$form->br();
86+
$form->input('submit', ['value' => 'Login']);
87+
88+
echo $doc;
89+
```
90+
91+
The output of the code would be similar to the following image.
92+
93+
<img src="tests/images/login-form.png">
94+
95+
### HTML/PHP Template Files
96+
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.
97+
98+
#### HTML Templates
99+
100+
Assume that we have HTML file with the following markup:
101+
``` html
102+
<!DOCTYPE html>
103+
<html>
104+
<head>
105+
<title>{{page-title}}</title>
106+
<meta charset="UTF-8">
107+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
108+
<meta name="description" content="{{page-desc}}">
109+
</head>
110+
<body>
111+
<section>
112+
<h1>{{page-title}}</h1>
113+
<p>
114+
Hello Mr.{{ mr-name }}. This is your visit number {{visit-number}}
115+
to our website.
116+
</p>
117+
</section>
118+
</body>
119+
</html>
120+
```
121+
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.
122+
``` php
123+
$document = HTMLNode::fromFile('my-html-file.html', [
124+
'page-title' => 'Hello Page',
125+
'page-desc' => 'A page that shows visits numbers.',
126+
'mr-name' => 'Ibrahim Ali',
127+
'visit-number' => 33,
128+
]);
129+
echo $document
130+
```
131+
The output of the above PHP code will be the following HTML code.
132+
``` html
133+
<!DOCTYPE html>
134+
<html>
135+
<head>
136+
<title>Hello Page</title>
137+
<meta charset="UTF-8">
138+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
139+
<meta name="description" content="A page that shows visits numbers.">
140+
</head>
141+
<body>
142+
<section>
143+
<h1>Hello Page</h1>
144+
<p>
145+
Hello Mr.Ibrahim Ali. This is your visit number 33
146+
to our website.
147+
</p>
148+
</section>
149+
</body>
150+
</html>
151+
```
152+
#### PHP Templates
153+
154+
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.
155+
156+
Assuming that we have the following PHP template that shows a list of posts titles:
157+
158+
``` php
159+
<div>
160+
<?php
161+
if (count($posts) != 0) {?>
162+
<ul>
163+
<?php
164+
foreach ($posts as $postTitle) {?>
165+
<li><?= $postTitle;?></li>
166+
<?php
167+
}
168+
?>
169+
</ul>
170+
<?php
171+
} else {
172+
echo "No posts.\n";
66173
}
67174
}
68175
```
@@ -80,7 +187,7 @@ Or add to your `composer.json`:
80187
<?php
81188
require_once 'vendor/autoload.php';
82189

83-
use WebFiori\UI\HTMLDoc;
190+
use WebFiori\Ui\HTMLDoc;
84191

85192
// Create a complete HTML5 document
86193
$doc = new HTMLDoc();
@@ -99,7 +206,7 @@ echo $doc;
99206
### Build Elements Programmatically
100207

101208
```php
102-
use WebFiori\UI\HTMLNode;
209+
use WebFiori\Ui\HTMLNode;
103210

104211
// Create a navigation menu
105212
$nav = new HTMLNode('nav', ['class' => 'main-nav']);
@@ -165,7 +272,7 @@ $parent->childrenCount(); // 1
165272
### Complete Document Structure
166273

167274
```php
168-
use WebFiori\UI\HTMLDoc;
275+
use WebFiori\Ui\HTMLDoc;
169276

170277
$doc = new HTMLDoc();
171278

@@ -227,7 +334,7 @@ $head->addChild('link', [
227334
### Element Creation and Manipulation
228335

229336
```php
230-
use WebFiori\UI\HTMLNode;
337+
use WebFiori\Ui\HTMLNode;
231338

232339
// Create elements with attributes
233340
$container = new HTMLNode('div', [
@@ -323,7 +430,7 @@ echo "Total items: " . $list->childrenCount(); // Direct method
323430
### Complete Form Creation
324431

325432
```php
326-
use WebFiori\UI\HTMLDoc;
433+
use WebFiori\Ui\HTMLDoc;
327434

328435
$doc = new HTMLDoc();
329436
$body = $doc->getBody();
@@ -470,7 +577,7 @@ $form->input('hidden', ['name' => 'form_id', 'value' => 'registration']);
470577
### Dynamic Data Tables
471578

472579
```php
473-
use WebFiori\UI\HTMLNode;
580+
use WebFiori\Ui\HTMLNode;
474581

475582
// Create responsive data table
476583
$tableContainer = new HTMLNode('div', ['class' => 'table-responsive']);
@@ -562,7 +669,7 @@ echo $tableContainer->toHTML(true);
562669
### Navigation Menus
563670

564671
```php
565-
use WebFiori\UI\HTMLNode;
672+
use WebFiori\Ui\HTMLNode;
566673

567674
// Main navigation
568675
$nav = new HTMLNode('nav', ['class' => 'navbar navbar-expand-lg navbar-dark bg-dark']);
@@ -644,7 +751,7 @@ echo $nestedList->toHTML(true);
644751
### Image Galleries and Media Components
645752

646753
```php
647-
use WebFiori\UI\HTMLNode;
754+
use WebFiori\Ui\HTMLNode;
648755

649756
// Hero section with background image
650757
$hero = new HTMLNode('section', ['class' => 'hero-section']);
@@ -754,7 +861,7 @@ Create reusable HTML templates with placeholder slots:
754861

755862
**Using the template:**
756863
```php
757-
use WebFiori\UI\HTMLNode;
864+
use WebFiori\Ui\HTMLNode;
758865

759866
$document = HTMLNode::fromFile('template.html', [
760867
'lang' => 'en',
@@ -845,7 +952,7 @@ echo $blogPost->toHTML(true);
845952
### CSS Management
846953

847954
```php
848-
use WebFiori\UI\HTMLNode;
955+
use WebFiori\Ui\HTMLNode;
849956

850957
$element = new HTMLNode('div');
851958

@@ -925,7 +1032,7 @@ echo $visibleOnMobile->toHTML(true);
9251032
WebFiori UI implements PHP's Iterator and Countable interfaces for seamless traversal:
9261033

9271034
```php
928-
use WebFiori\UI\HTMLNode;
1035+
use WebFiori\Ui\HTMLNode;
9291036

9301037
$menu = new HTMLNode('ul', ['class' => 'main-menu']);
9311038
$menu->li('Home');
@@ -963,7 +1070,7 @@ while ($menu->valid()) {
9631070
### XML Document Generation
9641071

9651072
```php
966-
use WebFiori\UI\HTMLNode;
1073+
use WebFiori\Ui\HTMLNode;
9671074

9681075
// Create SAML assertion
9691076
$assertion = new HTMLNode('saml:Assertion', [
@@ -1001,7 +1108,7 @@ echo $assertion->toXML(true);
10011108
### Optimizing HTML Output
10021109

10031110
```php
1004-
use WebFiori\UI\HTMLNode;
1111+
use WebFiori\Ui\HTMLNode;
10051112

10061113
// For production: Use unformatted output
10071114
$container = new HTMLNode('div');
@@ -1181,7 +1288,7 @@ WebFiori UI implements standard PHP interfaces:
11811288
<?php
11821289
require_once 'vendor/autoload.php';
11831290

1184-
use WebFiori\UI\HTMLDoc;
1291+
use WebFiori\Ui\HTMLDoc;
11851292

11861293
// Create a complete responsive web page
11871294
$doc = new HTMLDoc();

WebFiori/UI/Anchor.php renamed to WebFiori/Ui/Anchor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* https://github.com/WebFiori/.github/blob/main/LICENSE
99
*
1010
*/
11-
namespace WebFiori\UI;
11+
namespace WebFiori\Ui;
1212

1313
/**
1414
* A class that represents &lt;a&gt; tag.

WebFiori/UI/Br.php renamed to WebFiori/Ui/Br.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* https://github.com/WebFiori/.github/blob/main/LICENSE
99
*
1010
*/
11-
namespace WebFiori\UI;
11+
namespace WebFiori\Ui;
1212

1313
/**
1414
* A class that represents &lt;br&gt; tag.

WebFiori/UI/CodeSnippet.php renamed to WebFiori/Ui/CodeSnippet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* https://github.com/WebFiori/.github/blob/main/LICENSE
99
*
1010
*/
11-
namespace WebFiori\UI;
11+
namespace WebFiori\Ui;
1212

1313
/**
1414
* A class that can be used to display code snippets in good-looking way.

WebFiori/UI/Exceptions/InvalidNodeNameException.php renamed to WebFiori/Ui/Exceptions/InvalidNodeNameException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* https://github.com/WebFiori/.github/blob/main/LICENSE
99
*
1010
*/
11-
namespace WebFiori\UI\Exceptions;
11+
namespace WebFiori\Ui\Exceptions;
1212

1313
use Exception;
1414
/**

WebFiori/UI/Exceptions/TemplateNotFoundException.php renamed to WebFiori/Ui/Exceptions/TemplateNotFoundException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* https://github.com/WebFiori/.github/blob/main/LICENSE
99
*
1010
*/
11-
namespace WebFiori\UI\Exceptions;
11+
namespace WebFiori\Ui\Exceptions;
1212

1313
use Exception;
1414
/**

WebFiori/UI/HTMLDoc.php renamed to WebFiori/Ui/HTMLDoc.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
* https://github.com/WebFiori/.github/blob/main/LICENSE
99
*
1010
*/
11-
namespace WebFiori\UI;
11+
namespace WebFiori\Ui;
1212

1313
use WebFiori\Collections\LinkedList;
14-
use WebFiori\UI\Exceptions\InvalidNodeNameException;
14+
use WebFiori\Ui\Exceptions\InvalidNodeNameException;
1515
/**
1616
* A class that represents HTML document.
1717
*

WebFiori/UI/HTMLList.php renamed to WebFiori/Ui/HTMLList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* https://github.com/WebFiori/.github/blob/main/LICENSE
99
*
1010
*/
11-
namespace WebFiori\UI;
11+
namespace WebFiori\Ui;
1212

1313
/**
1414
* A class that represents ordered list or unordered list.

WebFiori/UI/HTMLNode.php renamed to WebFiori/Ui/HTMLNode.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
* https://github.com/WebFiori/.github/blob/main/LICENSE
99
*
1010
*/
11-
namespace WebFiori\UI;
11+
namespace WebFiori\Ui;
1212

1313
use Countable;
1414
use Iterator;
1515
use ReturnTypeWillChange;
1616
use WebFiori\Collections\LinkedList;
1717
use WebFiori\Collections\Stack;
18-
use WebFiori\UI\Exceptions\InvalidNodeNameException;
19-
use WebFiori\UI\Exceptions\TemplateNotFoundException;
18+
use WebFiori\Ui\Exceptions\InvalidNodeNameException;
19+
use WebFiori\Ui\Exceptions\TemplateNotFoundException;
2020
/**
2121
* A class that represents HTML element.
2222
*

0 commit comments

Comments
 (0)