airmanbzh/php-html-generator

PHP html generator

v1.0.2 2018-04-06 14:52 UTC

This package is auto-updated.

Last update: 2024-11-26 22:46:09 UTC


README

Create HTML tags and render them efficiently.

Build status:

  • Master Build Status
  • Dev Build Status

Overview

return HtmlTag::createElement();
// returns an empty HtmlTag Container
return HtmlTag::createElement('a');
// returns an HtmlTag containing a 'a' tag

Why you should use it

  • it always generates valid HTML and XHTML code
  • it makes templates cleaner
  • it's easy to use and fast to execute

Render tags

echo(HtmlTag::createElement('a'));

or

$tag = HtmlTag::createElement('a')
echo( $tag );

Simple tags

echo HtmlTag::createElement('div');
<div></div>
echo(HtmlTag::createElement('p')->text('some content'));
<p>some content</p>

Structured tags

echo(HtmlTag::createElement('div')->addElement('a')->text('a text'));
<div><a>a text</a></div>
$container = HtmlTag::createElement('div');
$container->addElement('p')->text('a text');
$container->addElement('a')->text('a link');
<div><p>a text</p><a>a link</a></div>

Attributes

Classics attributes (method : 'set')

$tag = HtmlTag::createElement('a')
    ->set('href','./sample.php')
    ->set('id','myID')
    ->text('my link');
echo( $tag );
<a href='./sample.php' id='myID'>my link</a>

Shortcut to set an ID attribute (method : 'id')

$tag = HtmlTag::createElement('div')
    ->id('myID');
echo( $tag );
<div id='myID'>my link</div>

Class management (method : 'addClass'/'removeClass')

$tag = HtmlTag::createElement('div')
    ->addClass('oneClass')
    ->text('my content')
echo( $tag );
<div class="oneClass">my content</div>
$tag = HtmlTag::createElement('div')
    ->addClass('aClass')
    ->addClass('anothereClass')
    ->text('my content')
echo( $tag );
<div class="aClass anothereClass">my content</div>
$tag = HtmlTag::createElement('div')
    ->addClass('firstClass')
    ->addClass('secondClass')
    ->text('my content')
    ->removeClass('firstClass');
echo( $tag );
<div class="secondClass">my content</div>

More

Text and content are generated according to the order of addition

$tag = HtmlTag::createElement('p')
    ->text('a text')
    ->addElement('a')
    ->text('a link');
<p>ma text<a>a link</a></p>

To generate content before text, 2 solutions :

$tag = HtmlTag::createElement('p')
    ->addElement('a')
    ->text('a link')
    ->getParent()
    ->text('a text');

or

$tag = HtmlTag::createElement('p');
$tag->addElement('a')->text('a link');
$tag->text('a text');
<p><a>a link</a>a text</p>