JBZoo Html
HTML helper for easy way form building from any PHP code., (*1)
, (*2)
Install
composer require jbzoo/html:"1.x-dev" # Last version
composer require jbzoo/html # Stable version
How to use?
require_once './vendor/autoload.php'; // composer autoload.php
// Get needed classes
use JBZoo\Html\Html;
echo Html::_('input')->render( /* ... */ ); // Underscore as factory
$html = Html::getInstance(); // Create or get pimple container
echo $html['input']->render( /* ... */ ); // As array
echo $html->input->render( /* ... */ ); // As object
echo Html::_('button')->render('test', 'Button', array('title' => 'title attr'));
```html
Button , (*3)
#### 1.2 Change button type
```php
echo Html::_('button')->render('test', 'Button', array(), 'reset');
```html
Button , (*4)
#### 1.3 Support UIkit CSS Framework button
```php
echo Html::_('button')->render('test', 'My button', array('button' => 'success', 'class' => 'my-class'));
```html
My button , (*5)
#### 1.4 Support UIkit CSS Framework icon
```php
echo Html::_('button')->render('test', 'My button', array('button' => 'success', 'icon' => 'stop'));
```html
My button
, (*6)
## 2# Boolean elements
------------------------------------------------------------------------------------------------------------------------
#### 2.1 Radio buttons
```php
echo Html::_('radiobool')->render('show', 0); // 0 is "No" and 1 is "Yes"
```html
No
Yes
, (*7)
#### 2.2 Single checkbox flag
```php
echo Html::_('checkbool')->render(
'show', // Name
0, // Checked value
array( // Attrs
'data-rel' => 'tooltip',
'data-title' => 'Checkbox title',
)
);
```html
type="checkbox"
name="show"
value="1" , (*8)
data-rel="tooltip" <!-- ... and custom attrs -->
data-title="Checkbox title"
class="jb-val-1"
> Yes
, (*9)
## 3# Elements with list of options
------------------------------------------------------------------------------------------------------------------------
#### 3.1 Default checkbox template (No wrap)
```php
echo Html::_('checkbox')->render(
array( // List of options
'val-1' => 'Label 1',
'val-2' => 'Custom Label',
'val-3' => 'Var 3',
),
'test', // Name
array('val-1', 'val-3'), // Checked values
array('data-rel' => 'tooltip') // Attrs
);
```html
Label 1
Custom Label
Var 3 , (*10)
#### 3.2 Default wrap checkbox template
```php
echo Html::_('checkbox')->render(
array( // List of options
'val-1' => 'Label 1',
'val-2' => 'Custom Label',
'val-3' => 'Var 3',
),
'test', // Name
array('val-1', 'val-3'), // Checked values
array('data-rel' => 'tooltip') // Attrs
);
```html
Label 1
Custom Label
Var 3
, (*11)
#### 3.3 Custom checkbox template
```php
echo Html::_('checkbox')->render(
array( // List of options
'val-1' => 'Label 1',
'val-2' => 'Custom Label',
'val-3' => 'Var 3',
),
'test', // Name
'val-1', // Checked value
array(), // Attrs
function ($list, $name, $value, $id, $text, $attrs) { // Custom render function
$alias = $value;
$inpClass = 'jb-val-' . $alias;
$input = $list->input($name, $value, $id, $inpClass, $attrs);
$text = '<span class="label-title">' . $text . '</span>';
$label = $list->label($id, $alias, $text);
return implode(PHP_EOL, array($input, $label));
}
);
```html
Label 1
Custom Label
Var 3
, (*12)
#### 3.3 Default checkbox template (No wrap)
```php
echo Html::_('radio')->render(
array( // List value
'val-1' => 'Label 1',
'val-2' => 'Custom Label',
'val-3' => 'Var 3',
),
'test', // Checked value
array('val-1', 'val-3'),
array('data-rel' => 'tooltip')
);
```html
Label 1
Custom Label
Var 3 , (*13)
#### 3.4 Default wrap checkbox template
```php
echo Html::_('radio')->render(
array( // List of options
'val-1' => 'Label 1',
'val-2' => 'Custom Label',
'val-3' => 'Var 3',
),
'test', // Name
'val-1', // Checked value
array(), // Attrs
true // Wrap input by label
);
```html
Label 1
Custom Label
Var 3
, (*14)
#### 3.5 Custom radio template
```php
echo Html::_('radio')->render(
array(
'val-1' => 'Label 1',
'val-2' => 'Custom Label',
'val-3' => 'Var 3',
),
'test',
'val-1',
array(),
function ($list, $name, $value, $id, $text, $attrs) {
$alias = $value;
$inpClass = 'jb-val-' . $alias;
$input = $list->input($name, $value, $id, $inpClass, $attrs);
$text = '<span class="label-title">' . $text . '</span>';
$label = $list->label($id, $alias, $text);
return implode(PHP_EOL, array($input, $label));
}
);
```html
Label 1
Custom Label
Var 3
, (*15)
#### 3.5 Checked value is not exists
```php
echo Html::_('radio')->render(
array( // List of options
'val-1' => 'Label 1',
'val-2' => 'Custom Label',
'val-3' => 'Var 3',
),
'test', // Name
'val-8', // Undefined value
array(), // Atrs
true // Wrap inputs by labels
);
```html
Label 1
Custom Label
Var 3
Undefined
, (*16)
## 3# Data list
------------------------------------------------------------------------------------------------------------------------
```php
echo Html::_('datalist')->render(
array( // Keys and Values
'Label' => 'Content text',
'Custom label' => 'List text',
),
array( // Attrs
'class' => 'test',
'id' => 'custom',
)
);
```html
, (*17)
Label
Content text
Custom label
List text
## 4# Selects
------------------------------------------------------------------------------------------------------------------------
#### 4.1 Simple select list
```php
echo Html::_('select')->render(
array(
'val-1' => 'Label 1',
'test' => 'Label test',
'custom' => 'Custom',
),
'test',
'custom',
array(
'data-title' => 'My form select',
)
);
```html
Label 1
Label test
Custom
, (*18)
#### 4.1 Multiple select
```php
echo Html::_('select')->render(
array(
'val-1' => 'Label 1',
'test' => 'Label test',
'custom' => 'Custom',
'simple' => 'Simple label',
'moscow' => 'Moscow',
),
'test',
array('test', 'moscow'),
array(
'multiple' => true,
)
);
```html
Label 1
Label test
Custom
Simple label
Moscow
, (*19)
#### 4.3 Selected option is not found
```php
/**
* If selected params is array. For check selected option taken last array var.
* If option is not exists created new option. See output.
*/
echo Html::_('select')->render(
array(
'val-1' => 'Label 1',
'test' => 'Label test',
'custom' => 'Custom',
'simple' => 'Simple label',
'moscow' => 'Moscow',
),
'test',
array('test', 'moscow', 'no-value')
);
```html
--No selected--
Label 1
Label test
Custom
Simple label
Moscow
, (*20)
#### 4.4 Select with groups
```php
echo Html::_('select')->render(
array(
'val-1' => 'Label 1',
'test' => 'Test label',
array(
'gr-test' => 'Group test 1',
'val-1' => 'No exits in group',
)
),
'test',
array('test', 'moscow', 'no-value')
);
```html
--No selected--
Label 1
Test label
Group test 1
, (*21)
## 5# Hiddens fields
------------------------------------------------------------------------------------------------------------------------
#### 5.1 Single hidden input
```php
echo Html::_('hidden')->render('image', 'my-value', 'my-class', 'unique', array('data-profile' => 'user-1'));
```html
, (*22)
#### 5.2 Group of hidden inputs
```php
echo Html::_('hidden')->group(array(
'my-name' => 'My name value',
'user' => 'Administrator',
'array' => array(
'value' => 'my value',
'class' => 'array-hidden',
'id' => 'hide-id',
),
));
```html
, (*23)
## 6# Iframe
------------------------------------------------------------------------------------------------------------------------
```php
echo Html::_('iframe')->render('http://my-site.com/page', 'my-class', 'my-id', array('data-rel' => 'my-iframe'));
```html
, (*24)
## 7# Textarea
------------------------------------------------------------------------------------------------------------------------
```php
echo Html::_('textarea')->render(
'test',
'Text area content',
'my-class',
'my-id',
array(
'data-rel' => 'tooltip',
'data-title' => 'Enter description',
)
);
```html
, (*25)
## 7# Custom tag
------------------------------------------------------------------------------------------------------------------------
```php
echo Html::_('tag')->render('My content', 'custom-class', 'unique', array(
'tag' => 'div',
'title' => 'Custom title'
));
```html
, (*26)
My content
## 8# Data attr builder
------------------------------------------------------------------------------------------------------------------------
```php
echo Html::_('input')->render(
'test',
'My value',
'',
'',
array('data' => array(
'test' => 'val',
'json' => array(
'param-1' => 'val-1',
'param-2' => 'val-2',
)
))
);
```html
, (*27)
## Unit tests and check code style
```sh
make
make test-all
License
MIT, (*28)