1. Go to this page and download the library: Download ui-awesome/html-helper library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
declare(strict_types=1);
namespace App;
use UIAwesome\Html\Helper\AttributeBag;
$attributes = ['id' => 'submit'];
// merge arrays (later values override; merge is raw — no key normalization or `null` filtering)
AttributeBag::merge(
$attributes,
[
'class' => ['btn', 'btn-primary'],
'type' => 'submit',
],
);
// get with fallback default
$type = AttributeBag::get($attributes, 'type', 'button');
// get with prefix normalization
$label = AttributeBag::get($attributes, 'label', null, 'aria-');
// remove unwanted keys
AttributeBag::remove($attributes, 'readonly');
// set values (closures are resolved)
AttributeBag::set($attributes, 'disabled', true);
AttributeBag::set($attributes, 'id', static fn () => 'submit');
// set one key (raw value)
AttributeBag::set($attributes, 'aria-label', 'Save');
// booleans for `aria`, `data`, and `on*` attributes are stored as literal strings
AttributeBag::set($attributes, 'expanded', true, 'aria-');
// $attributes['aria-expanded'] === 'true'
// set many keys at once (useful for trait-driven prefixed attributes)
AttributeBag::setMany(
$attributes,
[
'data-toggle' => 'modal',
'onclick' => 'handleClick()',
],
);
// remove a key explicitly
AttributeBag::remove($attributes, 'id');
declare(strict_types=1);
namespace App;
use UIAwesome\Html\Helper\Encode;
class User implements \Stringable
{
public function __construct(private string $name) {}
public function __toString(): string
{
return $this->name;
}
}
$user = new User('<John Doe>');
// automatically casts and safely encodes
echo Encode::content($user);
// <John Doe>
declare(strict_types=1);
namespace App;
use UIAwesome\Html\Helper\Attributes;
use UIAwesome\Html\Helper\Attributes;
$attributes = [
'class' => ['icon', ButtonType::PRIMARY],
'data-config' => ['key' => '<val>'],
'title' => '<Safe Title>',
];
// Get raw values (encode: `false`)
$rawAttributes = Attributes::normalizeAttributes($attributes, encode: false);
// [
// 'class' => 'icon btn-primary',
// 'data-config' => '{"key":"<val>"}'
// 'title' => '<Safe Title>',
// ]
// Perfect for DOMDocument
foreach ($rawAttributes as $name => $value) {
// DOMDocument handles the escaping automatically here
$domElement->setAttribute($name, $value);
}