ui-awesome/html-mixin
Reusable PHP traits for HTML rendering components: attribute management, content, templates, and prefix/suffix/label collections.
Maintainers
Requires
- php: ^8.3
- ui-awesome/html-helper: ^0.7@dev
Requires (Dev)
- infection/infection: ^0.32
- maglnet/composer-require-checker: ^4.1
- php-forge/coding-standard: ^0.1
- php-forge/support: ^0.3
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.1
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^12.5
- ui-awesome/html-interop: ^0.4@dev
Suggests
None
Provides
None
Conflicts
None
Replaces
None
BSD-3-Clause 799b284d7951728c2556586a17d80d79d422da11
phptemplatehtmlattributesmixincontenttraitslabelprefixsuffixui-awesomehtml-mixin
This package is auto-updated.
Last update: 2026-06-22 10:13:17 UTC
README
Html Mixin
๐ PHPUnit
๐ Mutation Testing
๐ PHPStan
A type-safe PHP mixin library for HTML tag rendering components
Build reusable components with traits for attributes, content, templates, and prefix/suffix management.
Features
๐ Feature OverviewInstallation
composer require ui-awesome/html-mixin:^0.5
Quick start
Managing HTML attributes with HasAttributes
The HasAttributes trait provides a fluent, immutable API for managing HTML attributes on elements. It supports enum
keys/values, closure-based values, additive updates with attributes(), explicit replacement with replaceAttributes(),
and null values for removing attributes.
<?php declare(strict_types=1); namespace App\Component; use UIAwesome\Html\Mixin\HasAttributes; final class MyComponent { use HasAttributes; } $component = new MyComponent(); $component = $component ->addAttribute('id', 'my-component') ->attributes(['class' => 'container', 'role' => 'main']) ->attributes(['data-state' => 'open', 'aria-label' => 'Close']) ->removeAttribute('role'); $component->getAttributes(); // ['id' => 'my-component', 'class' => 'container', 'data-state' => 'open', 'aria-label' => 'Close'] $component->getAttribute('id', 'default-id'); // 'my-component' $component->getAttribute('aria-label'); // 'Close' $replacement = $component->replaceAttributes(['id' => 'replacement']); $replacement->getAttributes(); // ['id' => 'replacement']
Managing content with encoding support
The HasContent trait handles both safe encoded content and raw HTML with XSS protection through Encode::content().
<?php declare(strict_types=1); namespace App\Component; use UIAwesome\Html\Mixin\HasContent; final class MyComponent { use HasContent; } $component = new MyComponent(); $encodedContent = $component ->content('<script>alert("XSS")</script>') ->getContent(); // <script>alert("XSS")</script> $component2 = new MyComponent(); $htmlContent = $component2 ->html('<strong>Raw HTML</strong>') ->getContent(); // <strong>Raw HTML</strong>
Custom templates with HasTemplate
Define custom rendering templates for your components using the HasTemplate trait.
<?php declare(strict_types=1); namespace App\Component; use UIAwesome\Html\Mixin\{HasContent, HasTemplate}; final class MyComponent { use HasContent; use HasTemplate; public function render(): string { return str_replace('{content}', $this->content, $this->template); } } $component = new MyComponent(); echo $component ->content('Card Content') ->template('<div class="card">{content}</div>') ->render(); // <div class="card">Card Content</div>
Prefix and suffix content with tag support
The HasPrefixCollection and HasSuffixCollection traits add content before and after your element, optionally wrapped
in tags with their own attributes.
Tag APIs now accept UnitEnum, so your components can use any project enum without a direct runtime dependency on
ui-awesome/html-interop.
<?php declare(strict_types=1); namespace App\Component; use UIAwesome\Html\Mixin\{HasContent, HasPrefixCollection, HasSuffixCollection}; enum InlineTag: string { case STRONG = 'strong'; case EM = 'em'; } final class MyComponent { use HasContent; use HasPrefixCollection; use HasSuffixCollection; public function render(): string { return $this->prefix . $this->content . $this->suffix; } } $component = new MyComponent(); echo $component ->content('Main Content') ->prefix('Prefix: ') ->prefixAttributes(['class' => 'prefix-badge']) ->prefixTag(InlineTag::STRONG) ->suffix(' :Suffix') ->suffixTag(InlineTag::EM) ->render(); // <strong class="prefix-badge">Prefix: </strong>Main Content<em> :Suffix</em>
Documentation
For detailed configuration options and advanced usage.
- ๐งช Testing Guide
- โฌ๏ธ Upgrade Guide
Package information
๐ PHP
๐ Latest Stable Version
๐ Total Downloads
Quality code
๐ Codecov
๐ PHPStan Level Max
๐ Super-Linter
๐ StyleCI
Our social networks
๐ Follow on X
๐ Follow on Facebook
