PHPackages                             patrickschur/html5gen - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. patrickschur/html5gen

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

patrickschur/html5gen
=====================

An easy to use HTML5 generator written in PHP. Creates valid and secure HTML5 code.

v1.1.0(8y ago)16272MITPHPPHP &gt;=5.5

Since Mar 12Pushed 7y ago1 watchersCompare

[ Source](https://github.com/patrickschur/html5gen)[ Packagist](https://packagist.org/packages/patrickschur/html5gen)[ Docs](https://github.com/patrickschur/html5gen)[ RSS](/packages/patrickschur-html5gen/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (4)Dependencies (1)Versions (5)Used By (0)

html5gen
========

[](#html5gen)

[![Build Status](https://camo.githubusercontent.com/b34561d6fc122727a752f27d13338069216165293fa28676006d35be62bcf240/68747470733a2f2f7472617669732d63692e6f72672f7061747269636b73636875722f68746d6c3567656e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/patrickschur/html5gen)[![codecov](https://camo.githubusercontent.com/80e8594e0c8c60e8326e986460a9619dbba5bc8628a8969761a5ab65b005cae9/68747470733a2f2f636f6465636f762e696f2f67682f7061747269636b73636875722f68746d6c3567656e2f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/patrickschur/html5gen)[![Version](https://camo.githubusercontent.com/476281bbe3382e45c551458fe9eb7a66d81b4a1a5b4dcb5cdf178c041eada1f1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7061747269636b73636875722f68746d6c3567656e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/patrickschur/html5gen)[![Minimum PHP Version](https://camo.githubusercontent.com/9c240c23a2a3937bfb7fbd55221fdc9a0a14a7680d34cd4bde4e00d8cbed314d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230352e352d46462e7376673f7374796c653d666c61742d737175617265)](http://php.net/)[![License](https://camo.githubusercontent.com/eb0cb5d4279af564c57a4817521f920c50c23cb08095cde4fa5ca6aaedfc9990/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7061747269636b73636875722f68746d6c3567656e2e7376673f7374796c653d666c61742d737175617265)](https://opensource.org/licenses/MIT)

An easy to use HTML5 generator written in PHP. Creates valid and secure HTML5 code.

Installation
------------

[](#installation)

To install the library I would recommend to use Composer. Execute the following command

```
$ composer require patrickschur/html5gen
```

or add this to your `composer.json`

```
{
  "require": {
    "patrickschur/html5gen": "*"
  }
}
```

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.

Usage
-----

[](#usage)

```
require 'vendor/autoload.php';

use Html5Gen\Html5Gen as H; // shorthand

echo
H::html([], function() {
    H::head();
    H::body();
});
```

outputs:

```
>

```

#### Attributes

[](#attributes)

All elements have one thing in common, all of them expect attributes. That is also the reason why it is the first parameter of all methods. You can specify attributes by an array. Where the key is the name of the attribute and the value the attribute value. If you do not want to specify attributes, leave the array empty. You can also omit the array if you do not use a callback.

```
echo
H::html(['lang' => 'en', 'dir' => 'ltr'], function() {
    H::head();
    H::body([], function () {
        H::p(['class' => 'foo bar']);
    });
});
```

outputs:

```
>

```

#### Callbacks

[](#callbacks)

The second and last parameter is the a callback function defined by the user. This lets you write a more generic code and allows the nesting of elements. With the `yield` keyword it is possible to write text into the current element without terminating the function.

> Notice: Elements like `base`, `br`, `meta`, `area`, `input`, `wbr`, `hr`, `link`, `param`, `source`, `col` and `img` can not have a callback, only attributes.

```
echo
H::html([], function() {
    H::head([], function () {
        H::meta(['charset' => 'UTF-8']);
    });
    H::body([], function ()
    {
        foreach (range(0, 10) as $number) {
            H::p([], function () use ($number) {
                yield $number;
            });
        }
    });
});
```

outputs:

```
>

0
1
2
3
4
5
6
7
8
9
10

```

#### Automatically Escaped Values

[](#automatically-escaped-values)

The script will automatically detect if it is useful to escape a value or not. 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. But if you use a `pre` tag it makes much more sense to escape the value. Also works for attributes.

```
echo
H::html([], function() {
    H::head([]);
    H::body([], function () {
        // A really bad attribute value
        H::pre(['id' => '"\'"'], function () {
            yield 'alert(1);'; // maybe some user code?
        });

        // Be careful with that and do not use user code in it
        H::script([], function () {
            yield 'var x = Math.floor(Math.random() * 100);';
            yield 'alert(0 < x < 100);';
        });
    });
});
```

outputs:

```
>

&lt;script&gt;alert(1);&lt;/script&gt;
var x = Math.floor(Math.random() * 100);alert(0
