PHP code example of ui-awesome / html-helper

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/ */

    

ui-awesome / html-helper example snippets




declare(strict_types=1);

namespace App;

use UIAwesome\Html\Helper\AttributeBag;

// normalize string key (adds prefix if missing)
echo AttributeBag::normalizeKey('label', 'aria-');
// aria-label

// normalize Enum key
echo AttributeBag::normalizeKey(Data::ACTION, 'data-');
// data-action

// normalize event (flexible prefixing)
echo AttributeBag::normalizeKey('click', 'on');
// onclick



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);
// &lt;John Doe&gt;



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);
}



declare(strict_types=1);

namespace App;

use UIAwesome\Html\Helper\CSSClass;

$attributes = ['class' => 'base-class'];

// add new classes (merges efficiently)
CSSClass::add($attributes, ['text-center', 'mt-5']);

// override existing classes
CSSClass::add($attributes, 'alert alert-danger', true);

echo $attributes['class'];
// alert alert-danger



declare(strict_types=1);

namespace App;

use UIAwesome\Html\Helper\Encode;

// safe Content
echo Encode::content('<script>alert("xss")</script>');
// &lt;script&gt;alert("xss")&lt;/script&gt;

// safe Attribute Value
echo Encode::value('Name "Quote"');
// Name &quot;Quote&quot;



declare(strict_types=1);

namespace App;

use App\Enums\Status;
use UIAwesome\Html\Helper\Enum;

// normalize array of Enums
$result = Enum::normalizeArray([Status::ACTIVE, Status::INACTIVE]);
// ['active', 'inactive']

// normalize mixed array
$result = Enum::normalizeArray(['foo', Status::ACTIVE, 42]);
// ['foo', 'active', 42]

// normalize value from Enum
Enum::normalizeValue(Status::ACTIVE);
// 'active'

// normalize value from mixed
Enum::normalizeValue('foo');
// 'foo'



declare(strict_types=1);

namespace App;

use UIAwesome\Html\Helper\Naming;

// generate input name (Nested)
echo Naming::generateInputName('User', 'profile[0][email]');
// User[profile][0][email]

// generate input ID (Sanitized)
echo Naming::generateInputId('User', 'profile[0][email]');
// user-profile-0-email

// convert regex to pattern
echo Naming::convertToPattern('/^[a-z]+$/i');
// ^[a-z]+$



declare(strict_types=1);

namespace App;

use UIAwesome\Html\Helper\Template;

echo Template::render("Hello, {name}!", ['{name}' => 'Yii3']);
// Hello, Yii3!



declare(strict_types=1);

namespace App;

use UIAwesome\Html\Helper\LineBreakNormalizer;

echo LineBreakNormalizer::normalize("Hello\n\n\nWorld");
// Hello\nWorld



declare(strict_types=1);

namespace App;

use UIAwesome\Html\Helper\Validator;

// validate integer-like string
$isValid = Validator::intLike('123', 0, 1000);

// validate against allowed list (supports Enums)
Validator::oneOf('sm', ['sm', 'md', 'lg'], 'size');
// passes

// validate positive-like number
$isPositive = Validator::positiveLike('42.5', 0, 100);
// true

// validate SVG offset-like values (ratio: 0..1, percent: 0%..100%)
$isOffsetValid = Validator::offsetLike('50%');
// true