nijhof / html-builder
Typed HTML builder for PHP.
v0.2.3
2026-10-06 12:29 UTC
Requires
- php: ^8.3
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Typed HTML builder for PHP.
HTML Builder provides small value objects and element classes for constructing escaped HTML in PHP code. It focuses on explicit element types, typed attributes, and safe text rendering.
Requirements
- PHP 8.3 or higher
Installation
composer require nijhof/html-builder
Example
<?php
declare(strict_types=1);
use Nijhof\Html\Elements\Div;
echo Div::of(Text::of('<content>'))->render();
Output:
<div><content></div>
Search page
Build a read-only search page with typed routes, field names and values. Text and attribute values are escaped when rendered.
use Nijhof\Html\Attributes\ElementId;
use Nijhof\Html\Elements\Button;
use Nijhof\Html\Elements\Form;
use Nijhof\Html\Elements\Input;
use Nijhof\Html\Elements\Table;
use Nijhof\Html\Elements\TableBody;
use Nijhof\Html\Elements\TableCell;
use Nijhof\Html\Elements\TableHead;
use Nijhof\Html\Elements\TableHeaderCell;
use Nijhof\Html\Elements\TableRow;
use Nijhof\Html\Text;
use Nijhof\Html\Values\ButtonType;
use Nijhof\Html\Values\FormFieldName;
use Nijhof\Html\Values\RouteUrl;
$query = Input::search(ElementId::of('query'), FormFieldName::of('query'))
->withPlaceholder('Search')
->withValue('Ada');
echo Form::get(
RouteUrl::of('/search'),
$query,
Button::text(ButtonType::SUBMIT, 'Search')
)->render();
echo Table::of(
TableHead::of(TableRow::of(TableHeaderCell::text('Name'))),
TableBody::of(TableRow::of(TableCell::text('Ada Lovelace')))
)->render();
Development
composer install
composer analyse
composer test
License
This package is licensed under the MIT License.