HTMLBuilder - best HTML generation class there is!
HTMLBuilder can be used to generate / build HTML using simple object-oriented interface., (*1)
Feature and usage run-through
Some code snippets that showcase the HTMLBuilder features and API., (*2)
Instance retrieval
To start building your HTML you first need to create HTMLBuilder object. Constructor doesn't need any arguments., (*3)
There are two ways that you can achieve this:, (*4)
- new keyword
- HTMLBuilder::dispatch() method
Both of them will achieve the same thing but HTMLBuilder::dispatch() method has the ability to be a one-liner (at least in PHP versions < 5.4)., (*5)
Initialization via new keyword
<?php
$html = new HTMLBuilder;
?>
Initialization via HTMLBuilder::dispatch() method
<?php
$html = HTMLBuilder::dispatch();
?>
HTML generation
Almost all instance methods will append a HTML tag to the generated HTML where tag name is method name. These methods support two arguments:
* content - content inside the tag
* attributes - tag attributes as key-value array where key is attribute name and value is attribute value, (*6)
Both arguments are optional. It's also possible to pass attributes as the only argument. Tag will automatically close., (*7)
To keep the tag open you have two options:
* use HTMLBuilder::open() method before the tag method
* use HTMLBuilder::open() method and pass tag name and attributes as arguments for this method, (*8)
php, (*9)
<?php
echo HTMLBuilder::dispatch()->p("Hello World");
?>
html, (*10)
<p>Hello World</p>
Using HTMLBuilder::open() before tag method
php, (*11)
<?php
echo HTMLBuilder::dispatch()->open()->div()->p("Hello World");
?>
html, (*12)
<div><p>Hello World</p></div>
Using HTMLBuilder::open() and pass tag arguments inside it
php, (*13)
<?php
echo HTMLBuilder::dispatch()->open("div")->p("Hello World");
?>
html, (*14)
<div><p>Hello World</p></div>