yiisoft/html
Tools for dynamic server-side HTML generation: rich set of tag classes, custom tags, widgets (ButtonGroup, CheckboxList, RadioList), automatic HTML-encoding with NoEncode bypass, and an Html helper with static methods to build tags and widgets.
## Getting Started
### Minimal Steps
1. **Installation**:
```bash
composer require yiisoft/html
Add to composer.json if using Laravel:
"require": {
"yiisoft/html": "^1.0"
}
First Use Case: Generate a simple link in a Laravel Blade template:
<?= \Yiisoft\Html\Html::a('Click Me', '/dashboard') ?>
Outputs: <a href="/dashboard">Click Me</a>
Key Entry Points:
Yiisoft\Html\Tag\* (e.g., Div, A, Form).Html::* methods (e.g., Html::button(), Html::form()).ButtonGroup, CheckboxList, RadioList.$link = (new \Yiisoft\Html\Tag\A())
->href('/profile')
->class('btn btn-primary')
->content('Profile');
echo $link; // Renders HTML
$card = (new \Yiisoft\Html\Tag\Div())
->class('card')
->content(
(new \Yiisoft\Html\Tag\H2())->content('Title'),
(new \Yiisoft\Html\Tag\P())->content('Content')
);
// Button
echo \Yiisoft\Html\Html::button('Submit', ['type' => 'submit']);
// Form
echo \Yiisoft\Html\Html::form(['action' => '/submit']);
echo \Yiisoft\Html\Html::textInput('username', 'John Doe');
echo \Yiisoft\Html::checkbox('agree', true, ['label' => 'Agree']);
$group = (new \Yiisoft\Html\Widget\ButtonGroup())
->buttons(
\Yiisoft\Html\Html::submitButton('Save'),
\Yiisoft\Html\Html::resetButton('Cancel')
)
->containerAttributes(['class' => 'btn-group']);
$list = (new \Yiisoft\Html\Widget\CheckboxList\CheckboxList('colors'))
->items(['red' => 'Red', 'blue' => 'Blue'])
->value(['red']);
Blade Directives: Create custom Blade directives for reusable components:
// app/Providers/BladeServiceProvider.php
Blade::directive('yiiform', function ($input) {
return "<?= \\Yiisoft\\Html\\Html::form($input); ?>";
});
Usage in Blade:
@yiiform(['action' => '/submit'])
Form Requests: Use yiisoft/html to generate form inputs dynamically:
public function buildForm(array $data, FormInterface $form) {
$form->add('name', \Yiisoft\Html\Html::textInput('name', $data['name']));
}
use Yiisoft\Html\NoEncode;
echo \Yiisoft\Html\Html::div(NoEncode::string('<b>Bold Text</b>'));
$alert = $errors ? (new \Yiisoft\Html\Tag\Div())
->class('alert alert-danger')
->content(implode('<br>', $errors))
: null;
echo $alert ?? '';
NoEncode or a tag object.
// Encoded: <script>alert('XSS')</script>
echo \Yiisoft\Html\Html::div('<script>alert("XSS")</script>');
// Not Encoded: <script>alert('XSS')</script>
echo \Yiisoft\Html\Html::div(NoEncode::string('<script>alert("XSS")</script>'));
echo \Yiisoft\Html\Html::div(\Yiisoft\Html\Html::b('Bold')); // <div><b>Bold</b></div>
i1685000000000).
require_once 'vendor/yiisoft/html/src/test-functions.php';
\Yiisoft\Html\IdGenerator\disableSeed();
\Yiisoft\Html\IdGenerator\reset(); // Next ID: i1
true/false for boolean attributes (e.g., disabled):
echo \Yiisoft\Html\Html::input('text', 'disabled', ['disabled' => true]);
data- or use the data method:
echo \Yiisoft\Html\Html::div(['data-user-id' => 123]);
// OR
echo (new \Yiisoft\Html\Tag\Div())->data('user-id', 123);
$link = new \Yiisoft\Html\Tag\A();
foreach ($items as $item) {
$link->href('/item/' . $item->id)->content($item->name);
echo $link;
}
render() only when needed (e.g., in Blade templates).class Card extends \Yiisoft\Html\Tag\Div {
public function header(string $content): self {
return $this->content(new \Yiisoft\Html\Tag\H2($content));
}
}
\Yiisoft\Html\Tag\Tag to customize encoding:
class SafeTag extends \Yiisoft\Html\Tag\Tag {
public function encode($content): string {
return $content; // Never encode
}
}
->render() to debug tag content:
$tag = (new \Yiisoft\Html\Tag\Div())->content('Test');
dd($tag->render()); // Debug output
getAttributes() to inspect attributes:
$button = \Yiisoft\Html\Html::button('Click');
dd($button->getAttributes()); // ['type' => 'button']
{!! \Yiisoft\Html\Html::div('<b>Raw</b>')->encode(false) !!}
asset() helper for local files:
echo \Yiisoft\Html\Html::cssFile(asset('css/app.css'));
render():
$tag = new \Yiisoft\Html\Tag\Div(); // Not rendered yet!
echo $tag; // Works (type cast)
// OR
echo $tag->render();
->attribute() instead of ->setAttribute() for clarity:
How can I help you explore Laravel packages today?