2017 © Pedro Peláez
 

library html-element

Html rendering in php inspired by hyperscript

image

spatie/html-element

Html rendering in php inspired by hyperscript

  • Friday, May 4, 2018
  • by Spatie
  • Repository
  • 12 Watchers
  • 96 Stars
  • 32,297 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 12 Forks
  • 1 Open issues
  • 8 Versions
  • 1 % Grown

The README.md

, (*1)

HtmlElement

Latest Version on Packagist Tests GitHub Code Style Action Status Total Downloads, (*2)

HtmlElement is a library to make dynamic HTML rendering more managable. The syntax is based on Hyperscript, and adds some Emmet-style syntactic sugar too., (*3)

Elements are rendered using the static HtmlElement::render method (which I recommend wrapping in a plain function for readability)., (*4)

el('div.container > div.row > div.col-md-6',
    el('a', ['href' => '#'], 'Hello world!')
);

```html , (*5)


## Support us [<img src="https://github-ads.s3.eu-central-1.amazonaws.com/html-element.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/html-element) We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). ## Postcardware You're free to use this package (it's [MIT-licensed](LICENSE.md)), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using. Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium. The best postcards will get published on the open source page on our website. ## Usage I recommend adding an `el` function to your application to improve readability over the static method. ```php function el(string $tag, $attributes = null, $content = null) : string { return \Spatie\HtmlElement\HtmlElement::render($tag, $attributes, $content); }

Examples

An empty tag:, (*6)

el('div');

```html , (*7)


A plain tag with text contents: ```php el('p', 'Hello world!');

```html , (*8)

Hello world!, (*9)


A tag with an attribute: ```php el('p', ['style' => 'color: red;'], 'Hello world!');

```html , (*10)

Hello world!, (*11)


A tag with an ID set emmet-style: ```php el('p#introduction', 'Hello world!');

```html , (*12)

Hello world!, (*13)


A tag with an emmet-style ID and class: ```php el('p#introduction.red', 'Hello world!');

```html , (*14)

Hello world!, (*15)


A tag with emmet-style attributes: ```php el('a[href=#][title=Back to top]', 'Back to top');

```html Back to top, (*16)


A more complex emmet-style abbreviation: ```php el('div.container > div.row > div.col-md-6', 'Hello world!');

```html , (*17)

Hello world!

Limited support of [implicit tag names](https://docs.emmet.io/abbreviations/implicit-names/) (`div`s only): ```php el('.container > .row > .col-md-6', 'Hello world!');

```html , (*18)

Hello world!

Manually nested tags: ```php el('div', ['class' => 'container'], el('nav', ['aria-role' => 'navigation'], '...') );

```html , (*19)


Multiple children: ```php el('ul', [el('li'), el('li')]);

```html , (*20)


Self-closing tags: ```php el('img[src=/background.jpg]');

```html , (*21)


```php el('img', ['src' => '/background.jpg'], '');

```html , (*22)


## Arguments The `el` function behaves differently depending on how many arguments are passed in. #### `el(string $tag) : string` When one argument is passed, only a tag will be rendered. ```php el('p');

```html , (*23)

, (*24)


--- #### `el(string $tag, string|array $contents) : string` When two arguments are passed, they represent a tag and it's contents. String example: ```php el('p', 'Hello world!');

```html , (*25)

Hello world!, (*26)


Array example: ```php el('ul', [el('li'), el('li')]);

```html , (*27)


--- #### `el(string $tag, array $attributes, string|array $contents) : string` When three arguments are passed, the first will be the tag name, the second an array of attributes, and the third a string or an array of contents. ```php el('div', ['class' => 'container'], el('nav', ['aria-role' => 'navigation'], '...') );

```html , (*28)


## Changelog Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. ## Testing ``` bash $ composer test

Contributing

Please see CONTRIBUTING for details., (*29)

Security

If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker., (*30)

Credits

About Spatie

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website., (*31)

License

The MIT License (MIT). Please see License File for more information., (*32)

The Versions

04/05 2018

dev-master

9999999-dev https://github.com/spatie/html-element

Html rendering in php inspired by hyperscript

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

spatie html-element

13/03 2017

1.0.1

1.0.1.0 https://github.com/spatie/html-element

Html rendering in php inspired by hyperscript

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

spatie html-element

11/03 2016

1.0.0

1.0.0.0 https://github.com/spatie/html-element

Html rendering in php inspired by hyperscript

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

spatie html-element

09/03 2016

0.5.0

0.5.0.0 https://github.com/spatie/html-element

Html rendering in php inspired by hyperscript

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

spatie html-element

08/03 2016

0.4.0

0.4.0.0 https://github.com/spatie/html-element

Html rendering in php inspired by hyperscript

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

spatie html-element

08/03 2016

0.3.0

0.3.0.0 https://github.com/spatie/html-element

Html rendering in php inspired by hyperscript

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

spatie html-element

07/03 2016

0.2.0

0.2.0.0 https://github.com/spatie/html-element

Html rendering in php inspired by hyperscript

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

spatie html-element

02/03 2016

0.1.0

0.1.0.0 https://github.com/spatie/html-element

Html rendering in php inspired by hyperscript

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

spatie html-element