You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -51,18 +51,125 @@ A PHP library for creating HTML documents and DOM manipulation with an object-or
51
51
52
52
## 🚀 Installation
53
53
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;
55
57
56
58
```bash
57
59
composer require webfiori/ui
58
60
```
59
61
60
62
Or add to your `composer.json`:
61
63
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.
$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
+
<imgsrc="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:
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.
<metaname="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";
66
173
}
67
174
}
68
175
```
@@ -80,7 +187,7 @@ Or add to your `composer.json`:
80
187
<?php
81
188
require_once 'vendor/autoload.php';
82
189
83
-
use WebFiori\UI\HTMLDoc;
190
+
use WebFiori\Ui\HTMLDoc;
84
191
85
192
// Create a complete HTML5 document
86
193
$doc = new HTMLDoc();
@@ -99,7 +206,7 @@ echo $doc;
99
206
### Build Elements Programmatically
100
207
101
208
```php
102
-
use WebFiori\UI\HTMLNode;
209
+
use WebFiori\Ui\HTMLNode;
103
210
104
211
// Create a navigation menu
105
212
$nav = new HTMLNode('nav', ['class' => 'main-nav']);
0 commit comments