keruyphp / keruy-html
Flexible fluent PHP DSL for HTML.
Requires
- php: ^8.3
README
Simple fluent PHP DSL for building HTML.
Installation
composer require keruyphp/keruy-html
Quick start
<?php declare(strict_types=1); require __DIR__ . '/vendor/autoload.php'; use KeruyPHP\KeruyHtml\Html; echo Html::div(['class' => 'box'], 'Hello world');
Passing content
You can build children in a few different ways.
1. As an array
The first array argument is treated as attributes. Pass an empty attribute array if you want to send children as an array.
echo Html::ul([], [ Html::li('One'), Html::li('Two'), Html::li('Three'), ]);
2. With a closure
echo Html::ul(function () { Html::li('One'); Html::li('Two'); Html::li('Three'); });
3. As trailing arguments
echo Html::ul( Html::li('One'), Html::li('Two'), Html::li('Three'), );
Output styles
You can render the same structure using an array or a closure.
Array-based output
echo Html::div(['class' => 'card'], [ Html::h2('Title'), Html::p('Text from array children.'), ]);
Closure-based output
echo Html::div(function () { Html::h2('Title'); Html::p('Text from closure children.'); });
Core methods
append()
append() is the universal child API. It accepts strings, nodes, arrays, generators and closures.
String values are treated as text and escaped automatically.
echo Html::div(function (\KeruyPHP\KeruyHtml\Tags\Tag $div) { $div->append( 'Hello ', Html::strong('world'), '!', ); });
text()
text() is the explicit text helper. It is a semantic alias for appending escaped string content.
echo Html::p(function (\KeruyPHP\KeruyHtml\Tags\Tag $p) { $p->text('Price: ', '100 UAH'); });
raw()
raw() appends trusted HTML fragments without escaping.
echo Html::div(function (\KeruyPHP\KeruyHtml\Tags\Tag $div) { $div->raw('<strong>Trusted HTML</strong>'); });
Use raw() only for HTML you already trust.
Setting attributes and classes
You can add attributes and classes in three common ways.
1. In the attribute array
echo Html::div([ 'class' => 'box', 'id' => 'main', ], 'Hello');
2. Inside a closure
echo Html::div(function (\KeruyPHP\KeruyHtml\Tags\Tag $div) { $div->class('box'); $div->prop('id', 'main'); Html::span('Hello'); });
3. At the end of the chain
echo Html::div('Hello') ->class('box') ->prop('id', 'main');
class()
The class() method is the main shortcut for CSS classes.
echo Html::div('Hello') ->class('box') ->class('has-background-light');
prop()
Use prop() for any attribute.
echo Html::a('Profile') ->prop('href', '/profile') ->prop('target', '_blank');
You can also combine it with regular attribute arrays:
echo Html::input(['type' => 'text']) ->prop('name', 'email') ->prop('placeholder', 'Email');
Pretty output
Call pretty() if you want formatted HTML instead of minified output.
echo Html::div(['class' => 'card'], function () { Html::h2('Title'); Html::p('Formatted output example.'); })->pretty();
Notes
- Strings are escaped automatically.
- Use
HtmlElement::raw()orHtmlUtil::raw()if you need raw HTML. - Use
HtmlElement::text()orHtmlUtil::text()if you want explicit escaped text.