Html_lib
, (*1)
A library for creating Html via chainable PHP objects., (*2)
Methods
<?php
require_once 'path/to/Html_lib/Html_Autoloader.php';
// Instantiation
$div = new Html; # defaults to an empty 'div'
$button = new Html('button'); # specify the tagName
$p = new Html('p', 'This is a paragraph with some content.'); # specify inner element text
// Set element id
$appDiv = (new Html)->id('app');
// Adding classes
$div = new Html; # create a new div
$div->addClass('item'); # add class .item
$div->addClasses(['col-lg-3', 'col-md-4', 'col-sm-6']); # add multiple classes using an array
// Adding attributes
$input = new Html('input'); # create a new input
$input->addAttribute('type', 'text'); # add type="text" attr
$input->addAttributes(['required' => true, 'pattern' => '[a-zA-Z]']); # add an array of attrs
// Adding styles
$p = new Html('p'); # create a new paragraph
$p->addStyle('color', 'red'); # set color to red
$p->addStyles(['font-size' => '1.2em', 'text-transform' => 'uppercase',]); # set an array of inline styles
// Chaining
$parentDiv = (new Html)
->addStyle('color', 'red')
->addAttributes([ 'data-toggle' => 'tooltip', 'title' => 'This is my tooltip!' ])
->addClass('awesome');
echo $parentDiv;
// Outputs:
//
// now lets give it child elements
$parentDiv->newChild('h3', 'Chaining');
$parentDiv->newChild('p', 'This is some awesome chaining!');
$childForm = $parentDiv->newChild('form')->addAttribute('method', 'POST');
$childForm->newChild('input')->addAttribute('type', 'text');
$childForm->newChild('button', 'Send')->addAttribute('type', 'submit');
echo $parentDiv;
// Outputs:
//
//
Chaining
//
This is some awesome chaining!, (*3)
//
//
// Static method element creation
echo Html::createElement('h1', 'Awesome, right?', [
'style' => 'font-family:Monaco;',
'class' => 'awesome heading',
]);
// Outputs:
//
Awesome, right?