Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/khaledsaikat/html-form
PHP composer library (classes) to generate html elements
https://github.com/khaledsaikat/html-form
checkbox composer div email form html html-element html-form input labels option password php php-html php-library radio select submit text xml
Last synced: about 2 hours ago
JSON representation
PHP composer library (classes) to generate html elements
- Host: GitHub
- URL: https://github.com/khaledsaikat/html-form
- Owner: khaledsaikat
- License: mit
- Created: 2015-12-01T23:19:19.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2020-01-18T11:36:22.000Z (almost 5 years ago)
- Last Synced: 2024-10-23T16:42:40.262Z (14 days ago)
- Topics: checkbox, composer, div, email, form, html, html-element, html-form, input, labels, option, password, php, php-html, php-library, radio, select, submit, text, xml
- Language: PHP
- Homepage:
- Size: 50.8 KB
- Stars: 8
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Html-Form
Lightweight PHP classes to generate html elements.
This package does not have any dependency other than php-5.4
- License: MIT
## Requirements
- PHP 5.4.0 or above
## Installation
### Composer
[Composer](https://getcomposer.org/) is a widely used dependency manager for PHP
packages. This Html-Form is available on Packagist as
[`user-meta/html`](https://packagist.org/packages/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](https://getcomposer.org/doc/00-intro.md)
documentation.To add this dependency using the command, run the following from within your
project directory:```
composer require user-meta/html
```## Get Started
A quick example of generating text input with Html-Form using composer:
```php
```
Almost all types of html element can be used. (e.g: button, email, div, p etc)
```php
echo Html::button('Submit Me');
echo Html::email('example email');
echo Html::div('example text');
echo Html::p('example text');
```Output:
```html
example textexample text
```## Usage
### Basic Usage
#### Accepted arguments
Most of the element accept two arguments:
- `$default` : Default value
- `$attributes` : Array of attributes
- `echo Html::text($default, attributes);`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);`#### using name, id and class
To assign name, id, class or any other attributes, use second arguments (`$attributes`)
A text field with default value, name, id and class attributes:
```php
echo Html::text('Example_Value', ['name' => 'Example_Name', 'id' => 'Example_ID', 'class' => 'Example_Class']);
```Output:
```html
```
#### Add attributes to element
You can also add any attributes into any element:
```php
echo Html::text('Example_Value', ['name' => 'Example_Name', 'data-example' => 'Example_Data']);
```Output:
```html
```
#### using required, readonly and disabled
```php
echo Html::email(null, ['name' => 'Email', 'required']);
echo Html::email(null, ['name' => 'Email', 'readonly']);
echo Html::email(null, ['name' => 'Email', 'disabled']);
```Output:
```html
```
#### Using label
```php
echo Html::email(null, [
'name' => 'Example_Name',
'label' => 'Email'
]);
``````php
echo Html::email(null, [
'name' => 'Example_Name',
'label' => [
'Example',
'class' => 'Class'
]
]);
```Output:
```html
```
```html
Example```
#### A div with id and class attributes:
```php
echo Html::div('example text', ['id' => 'Example_ID', 'class' => 'Example_Class']);
```Output:
```html
example text
```#### lebel with label text, id, class and for attributes
```php
echo Html::label('Some text', ['id' => 'ID', 'class' => 'Class', 'for' => 'For']);
```Output:
```html
Some text
```### Using checkbox
#### Simple checkbox with default checked
```php
echo Html::checkbox(true, ['name' => 'Name']);
echo Html::checkbox(true, ['name' => 'Name', 'value' => 'Value']);
```Output:
```html
```
Pass first argument as false for default unchecked. `echo Html::checkbox(false)`
#### List of checkbox
Create a list of checkboxes with default values
```php
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
```html
Dog
Cat
```
**To get array of values by POST or GET method**```php
echo Html::checkbox(['cat'], ['name' => 'Name[]', 'id' => 'ID'], ['dog' => 'Dog', 'cat' => 'Cat']);
```Output
```html
Dog
Cat
```### Using select / radio
#### Create a select with default value, name and id attributes
```php
echo Html::select(['cat'], ['name' => 'Name'], ['dog' => 'Dog', 'cat' => 'Cat']);
echo Html::select(['cat'], ['name' => 'Name'], ['dog', 'cat']);
```Output
```html
Dog
Catdog
cat```
#### Create a list of radio
```php
echo Html::radio(['cat'], ['name' => 'Name', 'id' => 'ID'], ['dog', 'cat']);
```Output
```html
dog
cat
```### Using collection
Several elements can be grouped together as collection
```php
$div = new Html('div');
$div->p('Hello World');
$div->text('example');
$div->add('Some plain text');
echo $div->render();
```Output:
```html
Hello World
Some plain text
```Collection uses `Html` constructor and accept two parameters.
- `$type` (optional): name of tag. (e.g. form, div)
- `$attributes` (optional): array of attributes#### Form example
Generating a form using collections:
```php
$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:
```html
Enter your email and password for login
Password
```
#### Nested collections
Generating html template using nested collections:
```php
$html = new Html('html');
$head = $html->import('head');
$head->title('Example Title');
$body = $html->import('body');
$body->p('Hello World');
echo $html->render();
``````html
Example Title
Hello World
```
#### Using as xml generator
```php
$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
```xml
The Da Vinci Code
Dan Brown
American
```
## Advanced
It is possible to create any html element by calling their name.
```php
echo Html::email('[email protected]');
echo Html::h1('Example Heading');
```Under the hood, we use `Html::input()` for input element and `Html::tag()` for html tag
Create an email input by using `input` method:
```php
echo Html::input('email', '[email protected]');
```Create h1 by using `tag` method:
```php
echo Html::tag('h1', 'Example Heading');
```### Add html to before and after elements
```php
echo Html::email('', ['_before' => 'Before', '_after' => 'After']);
```Output
```html
BeforeAfter
```### Enclose with another element.
```php
echo Html::email('', ['_enclose' => 'div']);
echo Html::email('', ['_enclose' => ['div', 'class' => 'Class']]);
```Output
```html
```### several way to set options for select / multiselect / radio / checkbox
```php
// 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
```html
audibmw
AudiBMW
Ferrari
Ferrari
```Mixing several way with one options array
```php
echo Html::select(null, [], [
'audi',
'bmw' => 'BMW',
'honda' => [
'Honda',
'data-origin' => 'Japan'
],
[
'value' => 'ferrari',
'label' => 'Ferrari',
'data-origin' => 'Italy'
]
]);
```Output
```html
audi
BMW
Honda
Ferrari```
Using numeric value
```php
echo Html::select(null, [], [2 => 'Two', 4 => 'Four']);
```
Output```html
Two
Four```
## Security
### Escaping Output
Escaping means stripping out unwanted data, like malformed HTML or script tags.
The library apply `esc_attr` to value attribute. `esc_url` to `href` and `src` attributes.