Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/aduh95/html-generator

HTML generator written in PHP, implementing most of the jQuery DOM manipulation API
https://github.com/aduh95/html-generator

Last synced: 1 day ago
JSON representation

HTML generator written in PHP, implementing most of the jQuery DOM manipulation API

Awesome Lists containing this project

README

        

# HTML Generator

This project aims to generate valid and XSS-safe HTML from friendly PHP commands. You can use some of the the [jQuery DOM manipulations](http://api.jquery.com/category/manipulation/) methods, because I missed some of them in PHP. The project is built on PHP's [DOM functions](http://php.net/manual/en/book.dom.php), though the performance are quite good.
The goal is to improve readability (even for people who do not know PHP at all) and make it easier to detect and to avoid XSS. You won't need the `?>` closing tag anymore!

If you think this librairy lacks a feature or have some bad design, feel free to contribute or to raise an issue.

## Installation

The easiest way: using [Composer](https://getcomposer.org)

```sh
composer require aduh95/html-generator
```

Then, you have to include Composer's autoload file at the begining of your scripts.

```php

```

If you don't use Composer (and really you should!), you can use the PHAR archive you'll find in the Releases section.

```php

```

You can also clone this repo and include the PHP classes which follow the [PSR-4](www.php-fig.org/psr/psr-4/).

## Getting started

I would recommend to subclass the `Document` class to include your usual html tags. As soon as I have time, I will put examples in the wiki.

Here is an overview of the main features:

```php
getDOMDocument()->formatOutput = true;

// Add attribute array-like
$doc->getHead()->appendChild($doc->createElement('meta'))['charset'] = 'uft-8';
// Or add attribute jQuery-like
$doc->getHead()->link()->attr('rel', 'icon')->attr(['type'=>'image/png', 'href'=>'/asset/icon.png']);

$doc()->p()->text('alert("no XSS!");') // add XSS-protected text easily
()->p()->append() // add children to an element with a simple method call
()->b('You are looking for something, aren\'t you?') // Add text content
()->br() // Auto closing tags are handled
()->a(
['href'=>'http://google.fr/', 'alt'=>'Search the "web"'], // An other method to add attributes
'YES, YOU CAN FIND IT!'
)->data('user-color', 'red') // as in jQuery, you can set a "data-" attribute
()->br()
()->smaller(['class'=>'alert alert-info'])->text('This link is sponsored.')
()
()->p('I ♥ Rock\'n\'Roll!')
->attr('test', 4)
->data('HTMLCamelCaseDataInformation', 'valid') // Transform CamelCase dataset to snake_case to match W3C standard
;

// List shortcut
$list = $doc()->ul();
$list[] = $doc->createTextNode('First ');
$list[] = $doc->createElement('b')->text('second one');
$list->append('third one');
$list->append()
()->li('fourth one');

// Table shortcut
$table = $doc()->table();
$table[] = ['This', 'is', 'a', 'row'];
$table[] = [$doc->createElement('td')->attr('rowspan', 3)->text('another'), 'one'];
$table
->append(['data', 'in', 'the', 'row'])
->append([['multi', 'row'], ['so', 'easy']]);

// This line is optionnal, the document will be automatically output at the end of ths script
echo $doc;
?>
```

This will output:

```html

My title

<script>alert("XSS!");</script>


You are looking for something, aren't you?
YES, YOU CAN FIND IT!
This link is sponsored.


I ♥ Rock'n'Roll!



  • First <item>

  • second one

  • third one

  • fourth one

This
is
a
row

another
one

data
in
the
row

multi
row

so
easy

```

This project is inspired from [`airmanbzh/php-html-generator`](https://github.com/Airmanbzh/php-html-generator) and [`wa72/htmlpagedom`](https://github.com/wasinger/htmlpagedom).