Wallogit.com
2017 © Pedro Peláez
Build html attributes through the ArrayObject API
This package can be used to build html tag attributes through a simple, array-based API., (*1)
Use composer:, (*2)
composer require flsouto/htattrs
Building html attributes:, (*3)
<?php
require_once('vendor/autoload.php');
$attrs = new FlSouto\HtAttrs();
$attrs['name'] = 'test';
$attrs['onclick'] = 'alert("Test!")';
echo "<a $attrs>Click me</a>";
The above code will produce:, (*4)
<a name="test" onclick="alert("Test!")">Click me</a>
Since the HtAttrs class extends php's native \ArrayObject, you could initialize it with an array:, (*5)
``` <?php, (*6)
require_once('vendor/autoload.php');, (*7)
$attrs = new FlSouto\HtAttrs([ 'name' => 'test', 'onclick' => 'alert("Test!")' ]);, (*8)
echo "Click me";, (*9)
The above code will produce:
Click me, (*10)
## The Especial Style Attribute The style attribute is always an instance of the FlSouto\HtAttrStyle class, which in turn produces a list of inline css properties when outputted:
<?php, (*11)
require_once('vendor/autoload.php');, (*12)
$attrs = new FlSouto\HtAttrs([ 'name' => 'test', 'onclick' => 'alert("Test!")' ]);, (*13)
$attrs['style']['color'] = 'black';, (*14)
// it's also possible to get the object: $style = $attrs['style']; $style['padding'] = '5px';, (*15)
echo "Click me";, (*16)
The above code outputs:
Click me ```, (*17)