Lightweight PHP classes to generate html elements., (*1)
This package does not have any dependency other than php-5.4, (*2)
Requirements
Installation
Composer
Composer is a widely used dependency manager for PHP
packages. This Html-Form is available on Packagist as
user-meta/html
and can be
installed by running the composer require
command. To enable Composer for you project, refer to the
project's Getting Started
documentation., (*3)
To add this dependency using the command, run the following from within your
project directory:, (*4)
composer require user-meta/html
Get Started
A quick example of generating text input with Html-Form using composer:, (*5)
<?php
require __DIR__ . '/vendor/autoload.php';
use UserMeta\Html\Html;
echo Html::text('example');
Output:, (*6)
<input type="text" value="example"/>
Almost all types of html element can be used. (e.g: button, email, div, p etc), (*7)
echo Html::button('Submit Me');
echo Html::email('example email');
echo Html::div('example text');
echo Html::p('example text');
Output:, (*8)
<input type="button" value="Submit Me"/>
<input type="email" value="example email"/>
<div>example text</div>
<p>example text</p>
Usage
Basic Usage
Accepted arguments
Most of the element accept two arguments:
- $default
: Default value
- $attributes
: Array of attributes
- echo Html::text($default, attributes);
, (*9)
For options element like select, radio, it accept third argument as $options
- $options
: Array of options. The array can contains key-value pair or only values
- echo Html::select($default, attributes, $options);
, (*10)
using name, id and class
To assign name, id, class or any other attributes, use second arguments ($attributes
), (*11)
A text field with default value, name, id and class attributes:, (*12)
echo Html::text('Example_Value', ['name' => 'Example_Name', 'id' => 'Example_ID', 'class' => 'Example_Class']);
Output:, (*13)
<input type="text" name="Example_Name" value="Example_Value" id="Example_ID" class="Example_Class"/>
Add attributes to element
You can also add any attributes into any element:, (*14)
echo Html::text('Example_Value', ['name' => 'Example_Name', 'data-example' => 'Example_Data']);
Output:, (*15)
<input type="text" name="Example_Name" value="Example_Value" data-example="Example_Data"/>
using required, readonly and disabled
echo Html::email(null, ['name' => 'Email', 'required']);
echo Html::email(null, ['name' => 'Email', 'readonly']);
echo Html::email(null, ['name' => 'Email', 'disabled']);
Output:, (*16)
<input type="email" name="Email" required="required"/>
<input type="email" name="Email" readonly="readonly"/>
<input type="email" name="Email" disabled="disabled"/>
Using label
echo Html::email(null, [
'name' => 'Example_Name',
'label' => 'Email'
]);
echo Html::email(null, [
'name' => 'Example_Name',
'label' => [
'Example',
'class' => 'Class'
]
]);
Output:, (*17)
<label>Email</label>
<input type="email" name="Example_Name"/>
<label class="Class">Example</label>
<input type="email" name="Example_Name"/>
A div with id and class attributes:
echo Html::div('example text', ['id' => 'Example_ID', 'class' => 'Example_Class']);
Output:, (*18)
<div id="Example_ID" class="Example_Class">example text</div>
lebel with label text, id, class and for attributes
echo Html::label('Some text', ['id' => 'ID', 'class' => 'Class', 'for' => 'For']);
Output:, (*19)
<label id="ID" class="Class" for="For">Some text</label>
Using checkbox
Simple checkbox with default checked
echo Html::checkbox(true, ['name' => 'Name']);
echo Html::checkbox(true, ['name' => 'Name', 'value' => 'Value']);
Output:, (*20)
<input type="checkbox" name="Name" value="1" checked="checked"/>
<input type="checkbox" name="Name" value="Value" checked="checked"/>
Pass first argument as false for default unchecked. echo Html::checkbox(false)
, (*21)
List of checkbox
Create a list of checkboxes with default values, (*22)
echo Html::checkbox('cat', ['name' => 'Name', 'id' => 'ID'], ['dog' => 'Dog', 'cat' => 'Cat']);
echo Html::checkbox(['cat'], ['name' => 'Name', 'id' => 'ID'], ['dog' => 'Dog', 'cat' => 'Cat']);
Output, (*23)
<label><input type="checkbox" value="dog" name="Name" id="ID_1"/> Dog</label>
<label><input type="checkbox" value="cat" name="Name" id="ID_2" checked="checked"/> Cat</label>
To get array of values by POST or GET method, (*24)
echo Html::checkbox(['cat'], ['name' => 'Name[]', 'id' => 'ID'], ['dog' => 'Dog', 'cat' => 'Cat']);
Output, (*25)
<label><input type="checkbox" value="dog" name="Name[]" id="ID_1"/> Dog</label>
<label><input type="checkbox" value="cat" name="Name[]" id="ID_2" checked="checked"/> Cat</label>
Using select / radio
Create a select with default value, name and id attributes
echo Html::select(['cat'], ['name' => 'Name'], ['dog' => 'Dog', 'cat' => 'Cat']);
echo Html::select(['cat'], ['name' => 'Name'], ['dog', 'cat']);
Output, (*26)
<select name="Name">
<option value="dog">Dog</option>
<option value="cat" selected="selected">Cat</option>
</select>
<select name="Name">
<option value="dog">dog</option>
<option value="cat" selected="selected">cat</option>
</select>
Create a list of radio
echo Html::radio(['cat'], ['name' => 'Name', 'id' => 'ID'], ['dog', 'cat']);
Output, (*27)
<label><input type="radio" value="dog" name="Name" id="ID_1"/> dog</label>
<label><input type="radio" value="cat" name="Name" id="ID_2" checked="checked"/> cat</label>
Using collection
Several elements can be grouped together as collection, (*28)
$div = new Html('div');
$div->p('Hello World');
$div->text('example');
$div->add('Some plain text');
echo $div->render();
Output:, (*29)
<div>
<p>Hello World</p>
<input type="text" value="example"/>
Some plain text
</div>
Collection uses Html
constructor and accept two parameters.
- $type
(optional): name of tag. (e.g. form, div)
- $attributes
(optional): array of attributes, (*30)
Generating a form using collections:, (*31)
$form = new Html('form', ['method' => 'POST']);
$form->div('Enter your email and password for login');
$form->email('', ['name' => 'email', 'label' => 'Email']);
$form->password('', ['name' => 'password', 'label' => 'Password']);
$form->submit('login');
echo $form->render();
Output:, (*32)
<form method="POST">
<div>Enter your email and password for login</div>
<label>Email</label>
<input type="email" name="email"/>
<label>Password</label>
<input type="password" name="password"/>
<input type="submit" value="login"/>
</form>
Nested collections
Generating html template using nested collections:, (*33)
$html = new Html('html');
$head = $html->import('head');
$head->title('Example Title');
$body = $html->import('body');
$body->p('Hello World');
echo $html->render();
<html>
<head>
<title>Example Title</title>
</head>
<body>
<p>Hello World</p>
</body>
</html>
Using as xml generator
$book = new Html('book');
$book->title('The Da Vinci Code');
$author = $book->import('author');
$author->name('Dan Brown');
$author->nationality('American');
echo $book->render();
Output, (*34)
<book>
<title>The Da Vinci Code</title>
<author>
<name>Dan Brown</name>
<nationality>American</nationality>
</author>
</book>
Advanced
It is possible to create any html element by calling their name., (*35)
echo Html::email('noreply@gmail.com');
echo Html::h1('Example Heading');
Under the hood, we use Html::input()
for input element and Html::tag()
for html tag, (*36)
Create an email input by using input
method:, (*37)
echo Html::input('email', 'noreply@gmail.com');
Create h1 by using tag
method:, (*38)
echo Html::tag('h1', 'Example Heading');
Add html to before and after elements
echo Html::email('', ['_before' => 'Before', '_after' => 'After']);
Output, (*39)
Before<input type="email"/>After
Enclose with another element.
echo Html::email('', ['_enclose' => 'div']);
echo Html::email('', ['_enclose' => ['div', 'class' => 'Class']]);
Output, (*40)
<div>
<input type="email"/>
</div>
<div class="Class">
<input type="email"/>
</div>
several way to set options for select / multiselect / radio / checkbox
// Same value and label
echo Html::select(null, [], ['audi', 'bmw']);
// Different value and label
echo Html::select(null, [], ['audi' => 'Audi', 'bmw' => 'BMW']);
// Option with extra attributes
echo Html::select(null, [], ['ferrari' => ['Ferrari', 'data-origin' => 'Italy']]);
echo Html::select(null, [], [['value' => 'ferrari', 'label' => 'Ferrari', 'data-origin' => 'Italy']]);
Output, (*41)
<select><option value="audi">audi</option><option value="bmw">bmw</option></select>
<select><option value="audi">Audi</option><option value="bmw">BMW</option></select>
<select><option value="ferrari" data-origin="Italy">Ferrari</option></select>
<select><option value="ferrari" data-origin="Italy">Ferrari</option></select>
Mixing several way with one options array, (*42)
echo Html::select(null, [], [
'audi',
'bmw' => 'BMW',
'honda' => [
'Honda',
'data-origin' => 'Japan'
],
[
'value' => 'ferrari',
'label' => 'Ferrari',
'data-origin' => 'Italy'
]
]);
Output, (*43)
<select>
<option value="audi">audi</option>
<option value="bmw">BMW</option>
<option value="honda" data-origin="Japan">Honda</option>
<option value="ferrari" data-origin="Italy">Ferrari</option>
</select>
Using numeric value, (*44)
echo Html::select(null, [], [2 => 'Two', 4 => 'Four']);
Output, (*45)
<select>
<option value="2">Two</option>
<option value="4">Four</option>
</select>
Security
Escaping Output
Escaping means stripping out unwanted data, like malformed HTML or script tags., (*46)
The library apply esc_attr
to value attribute. esc_url
to href
and src
attributes., (*47)