yiisoft/html
Yii HTML is a PHP library for safe, dynamic server-side HTML generation. It provides classes for common HTML tags, a CustomTag builder, widgets like ButtonGroup/CheckboxList/RadioList, automatic content encoding with NoEncode, and an Html helper API.
Pros:
Tag classes like Div, A, Form), aligning well with Laravel’s service-oriented architecture. This enables granular control over HTML generation, reducing template clutter.Div()->id('foo')->class('bar')) improve readability and maintainability, especially in Blade templates or dynamic UI generation.Form, Html) but offers stricter typing and modern PHP features (e.g., PHP 8.1+ support, enums).Cons:
Form::open() or Blade directives may need to adapt to the package’s object-oriented syntax.NoEncode for raw HTML) may conflict with Laravel’s Blade auto-escaping unless explicitly configured.View::share(), Blade directives, or Form request handling.High: The package is PHP 8.1+ compatible and uses Composer, making integration seamless with Laravel’s dependency management.
@php echo \Yiisoft\Html\Tag\Div()->content('Hello'); @endphp) or wrapped in custom Blade directives for cleaner syntax.Html::div()) or bind tag classes to the container.Form requests but requires manual mapping of widget outputs (e.g., CheckboxList) to request validation rules.Potential Frictions:
Html helper and this package’s Html class may cause conflicts. Renaming the facade or namespace (e.g., YiiHtml) is recommended.Validator) doesn’t natively understand the package’s widget outputs (e.g., CheckboxList). Custom validation rules or request transformations may be needed.Low to Medium:
Mitigation Strategies:
Html::yiisoft()->div()).Div, A) before adopting widgets (e.g., CheckboxList).Use Case Alignment:
Html/Form helpers entirely, or supplement them for specific needs (e.g., complex widgets)?Form::macro()) that the package lacks, requiring custom extensions?Team Adoption:
Long-Term Maintenance:
Performance:
Html::tag() or Blade syntax?Security:
NoEncode) be handled?Script) that need integration with Laravel’s CSP middleware?@yiisoftDiv).CheckboxList values).href), so Laravel’s URL helpers (route(), asset()) can be passed directly.CheckboxList can enhance dynamic forms, but event handling may require custom JavaScript.addCssClass()) align well with utility-first CSS.Pilot Phase:
Html::tag('div')) with the package’s Div class in a single feature/module.// Before (Laravel)
{!! Html::tag('div', ['class' => 'container'], 'Hello') !!}
// After (yiisoft/html)
<?= \Yiisoft\Html\Tag\Div::class('container')->content('Hello') ?>
Facade Integration:
// app/Providers/AppServiceProvider.php
public function register()
{
app()->bind('yiisoft.html', function () {
return new \Yiisoft\Html\Html();
});
}
// app/Facades/YiiHtml.php
public static function div(string $content = '', array $attributes = []): string
{
return (new \Yiisoft\Html\Tag\Div())->attributes($attributes)->content($content);
}
@yiisoftDiv('Hello', ['class' => 'container'])
Widget Adoption:
CheckboxList/RadioList.// Before
@foreach ($options as $value => $label)
<label>
<input type="checkbox" name="tags[]" value="{{ $value }}">
{{ $label }}
</label>
@endforeach
// After
{!! \Yiisoft\Html\Widget\CheckboxList\CheckboxList::make('tags')
->items($options)
->value(array_keys($selected))
!!}
FormRequest:
public function rules()
{
return [
'tags' => 'array',
'tags.*' => 'string',
];
}
Blade Directives:
// app/Providers/BladeServiceProvider.php
Blade::directive('yiisoftDiv', function ($expression) {
return "<?= (new \\Yiisoft\\Html\\Tag\\Div())->content({$expression}) ?>";
});
@yiisoftDiv('Dynamic content')
{!! !!} syntax for raw HTML or @php @endphp for encoded content.CheckboxList arrays).HtmlString or Stringable for testing rendered output.Html::tag()) with package classes in non-critical templates.How can I help you explore Laravel packages today?