Skip to content

Commit 6b6b070

Browse files
committed
Initial commit
0 parents  commit 6b6b070

File tree

8 files changed

+451
-0
lines changed

8 files changed

+451
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/
2+
composer.lock
3+
/.idea/

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Change Log
2+
All notable changes to this project will be documented in this file.

CONTRIBUTING.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Contributing
2+
3+
Feel free to contribute. Any help is welcome.
4+
5+
## License
6+
7+
By contributing to this, you agree that your contributions will be licensed under its [MIT license](LICENSE).
8+
9+
Thanks for contributing!

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Patrick Schur
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
html5gen
2+
=
3+
An easy to use HTML5 generator written in PHP. Creates valid and secure HTML5 code.
4+
5+
## Installation
6+
To install the library I would recommend to use Composer. Execute the following command
7+
```bash
8+
$ composer require patrickschur/html5gen
9+
```
10+
or add this to your `composer.json`
11+
```json
12+
{
13+
"require": {
14+
"patrickschur/html5gen": "*"
15+
}
16+
}
17+
```
18+
Because the script is not really big you could also copy the `Html5Gen.php` out of the `src` folder to use the script without Composer.
19+
20+
## Usage
21+
```php
22+
require 'vendor/autoload.php';
23+
24+
use Html5Gen\Html5Gen as H; // shorthand
25+
26+
echo
27+
H::html([], function() {
28+
H::head();
29+
H::body();
30+
});
31+
```
32+
outputs:
33+
```html
34+
<!DOCTYPE html>
35+
<html>
36+
<head></head>
37+
<body></body>
38+
</html>
39+
```
40+
41+
#### Attributes
42+
All elements have one thing in common, all of them expect attributes.
43+
That is also the reason why it is the first parameter of all methods.
44+
You can specify attributes by an array. Where the key is the name of the attribute and the value the attribute value.
45+
If you do not want to specify attributes, leave the array empty.
46+
You can also omit the array if you do not use a callback.
47+
```php
48+
echo
49+
H::html(['lang' => 'en', 'dir' => 'ltr'], function() {
50+
H::head();
51+
H::body([], function () {
52+
H::p(['class' => 'foo bar']);
53+
});
54+
});
55+
```
56+
outputs:
57+
```html
58+
<!DOCTYPE html>
59+
<html lang="en" dir="ltr">
60+
<head></head>
61+
<body><p class="foo bar"></p></body>
62+
</html>
63+
```
64+
65+
#### Callbacks
66+
The second and last parameter is the a callback function defined by the user.
67+
This lets you write a more generic code and allows the nesting of elements.
68+
With the `yield` keyword it is possible to write text into the current element without terminating the function.
69+
> Notice: Elements like `base`, `br`, `meta`, `area`, `input`, `wbr`, `hr`, `link`, `param`, `source`, `col` and `img` can not have a callback, only attributes.
70+
71+
```php
72+
echo
73+
H::html([], function() {
74+
H::head([], function () {
75+
H::meta(['charset' => 'UTF-8']);
76+
});
77+
H::body([], function ()
78+
{
79+
foreach (range(0, 10) as $number) {
80+
H::p([], function () use ($number) {
81+
yield $number;
82+
});
83+
}
84+
});
85+
});
86+
```
87+
outputs:
88+
```html
89+
<!DOCTYPE html>
90+
<html>
91+
<head><meta charset="UTF-8"></head>
92+
<body>
93+
<p>0</p>
94+
<p>1</p>
95+
<p>2</p>
96+
<p>3</p>
97+
<p>4</p>
98+
<p>5</p>
99+
<p>6</p>
100+
<p>7</p>
101+
<p>8</p>
102+
<p>9</p>
103+
<p>10</p>
104+
</body>
105+
</html>
106+
```
107+
108+
#### Automatically Escaped Values
109+
The script will automatically detect if it is useful to escape a value or not.
110+
For example if you want to define a `script` tag, it would not be useful to escape, because that destroys the logic of a script.
111+
But if you use a `pre` tag it makes much more sense to escape the value. Also works for attributes.
112+
113+
```php
114+
echo
115+
H::html([], function() {
116+
H::head([]);
117+
H::body([], function () {
118+
// A really bad attribute value
119+
H::pre(['id' => '"\'"'], function () {
120+
yield '<script>alert(1);</script>'; // maybe some user code?
121+
});
122+
123+
// Be careful with that and do not use user code in it
124+
H::script([], function () {
125+
yield 'var x = Math.floor(Math.random() * 100);';
126+
yield 'alert(0 < x < 100);';
127+
});
128+
});
129+
});
130+
```
131+
outputs:
132+
```html
133+
<!DOCTYPE html>
134+
<html>
135+
<head></head>
136+
<body>
137+
<pre id="&quot;'&quot;">&lt;script&gt;alert(1);&lt;/script&gt;</pre>
138+
<script>var x = Math.floor(Math.random() * 100);alert(0 < x < 100);</script>
139+
</body>
140+
</html>
141+
```
142+
143+
#### [TODO] Checklist:
144+
- [ ] Exists the attribute for the element?
145+
- [ ] Is the given attribute value allowed for the specified attribute?
146+
- [ ] Is the element allowed in the current element?
147+
- [ ] Check the document for bad practices. For example duplicate id references.
148+
- [ ] Check the `nonce` and `integrity` attribute on the correctness.
149+
150+
## Contributing
151+
Feel free to contribute. Any help is welcome.
152+
153+
## License
154+
This projects is licensed under the terms of the MIT license.

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "patrickschur/html5gen",
3+
"type": "library",
4+
"description": "An easy to use HTML5 generator written in PHP. Creates valid and secure HTML5 code.",
5+
"keywords": ["html", "html-builder", "php", "builder", "generator"],
6+
"homepage": "https://github.com/patrickschur/html5gen",
7+
"license": "MIT",
8+
"minimum-stability": true,
9+
"prefer-stable": "stable",
10+
"authors": [
11+
{
12+
"name": "Patrick Schur",
13+
"email": "patrick_schur@outlook.de"
14+
}
15+
],
16+
"autoload": {
17+
"Html5Gen\\": "src/Html5Gen"
18+
},
19+
"require": {
20+
"php": ">=5.5"
21+
},
22+
}

0 commit comments

Comments
 (0)