A helper package for generating HTML5 tags
The easiest way to install this package is using composer:
composer require studiow/htmlGenerating HTML tags with this package is pretty easy:
// Create the element
$link = new \Studiow\HTML\Element("a", "Documents", ["href"=>"/documents"]);
// Output
echo (string) $link;
// prints <a href="proxy.php?url=https%3A%2F%2Fgithub.com%2Fdocuments">Documents</a>Working with attributes is easy too:
// Create the element
$link = new \Studiow\HTML\Element("a", "Documents", ["href"=>"/documents"]);
// Set an attribute
$link->setAttribute('title', 'Go to documents');
// Get the value for an attribute
$link->getAttribute('title'); // returns 'Go to documents'
$link->getAttribute('attr_not_set'); // returns null
// Remove an attribute
$link->removeAttribute('title');Working with classes works pretty much as you'd expect:
// Create the element
$link = new \Studiow\HTML\Element("a", "Documents", ["href"=>"/documents"]);
// Add a single class
$link->addClass("button");
// Add multiple classes seperated by a space
$link->addClass("button button-documents");
// Remove a class
$link->removeClass("button");
// Check if the element has a certain class
$link->hasClass("button-documents");You can go ahead and chain the methods together
$link = new Studiow\HTML\Element("a");
$link->setInnerHTML("Documents")
->setAttribute("href", "/documents")
->addClass("button")
->addClass("button-documents")
->setAttribute('title', "Go to documents");
echo (string) $link; // Outputs <a href="proxy.php?url=https%3A%2F%2Fgithub.com%2Fdocuments" class="button button-documents" title="Go to documents">Documents</a>HTML5 attributes are supposed to be case-insensitive, but here they are case-sensitive. Lowercase recommended!
This package is by no means meant as a tool to generate large pieces of HTML. While you can use an Element as the innerHTML of another Element it will be converted to text when you do this.
If you find yourself rendering large pieces of HTML within a PHP script, you'd probably be better of using a template system.