Skip to content

ui-awesome/html-helper

Repository files navigation

Yii Framework

Html helper


PHPUnit Mutation Testing PHPStan

A powerful PHP library to simplify HTML element creation
Generate attributes, encode content, sanitize HTML, and manage CSS classes with ease.

Features

Feature Overview

Installation

composer require ui-awesome/html-helper:^0.3

Quick start

Rendering HTML attributes

Attributes helper sorts attributes by priority, handles boolean values, and automatically encodes JSON for complex data.

<?php

declare(strict_types=1);

namespace App;

use UIAwesome\Html\Helper\Attributes;
?>

<?= Attributes::render(
    [
        'id' => 'submit-btn',
        // automatically joined
        'class' => ['btn', 'btn-primary'],
        // `true` booleans are rendered as valueless attributes
        'disabled' => true,
        // `false` booleans are skipped
        'readonly' => false,
        // JSON encoded automatically
        'data' => [
            'id' => 42,
            'options' => ['modal' => true],
        ],
        'style' => [
            'color' => '#fff',
            'margin-top' => '10px'
        ],
    ]
) ?>
// output:
// class="btn btn-primary" id="submit-btn" disabled data-id="42" data-options='{"modal":true}' style='color: #fff; margin-top: 10px;'

Managing CSS classes

CSSClass helper allows you to merge, add, or override CSS classes within an attributes array smartly.

<?php

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'];
// output: alert alert-danger

Encoding

Encode helper ensures your content and attribute values are safe from XSS.

<?php

declare(strict_types=1);

namespace App;

use UIAwesome\Html\Helper\Encode;

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

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

Form naming & IDs

Naming helper generates standard PHP form names and valid HTML IDs, handling arrayable and nested properties effortlessly.

<?php

declare(strict_types=1);

namespace App;

use UIAwesome\Html\Helper\Naming;

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

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

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

Template rendering

Template helper performs clean token replacement with normalized line endings.

<?php

declare(strict_types=1);

namespace App;

use UIAwesome\Html\Helper\Template;

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

Validation

Validator helper enforces strict types and approved values for your HTML logic.

<?php

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

Documentation

For detailed configuration options and advanced usage.

Package information

PHP Latest Stable Version Total Downloads

Quality code

Codecov PHPStan Level Max Super-Linter StyleCI

Our social networks

Follow on X

License

License

About

HTML Helper for PHP.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages