Skip to content

berry

No more context-switching, just build your HTML templates in PHP.

Berry is a PHP library for composing correct HTML strings with small element builders.

The goal is to make the correct thing the easy thing: escaped text, valid nesting through element composition, typed helpers for common attributes, and view code your PHP tools can actually understand.

It is designed for the hypermedia-focused style that tools like HTMX and Datastar push: render HTML on the server, send HTML over the wire, and let the browser update the page from that.

This also works nicely with Locality of Behaviour: the markup, attributes, and server-side component that describe a piece of UI can live in one place, instead of being spread across templates, controllers, and separate JavaScript glue.

use function Berry\Html\{button, div, h1};
echo div()
->class('card')
->child(h1()->text('Hello Berry'))
->child(button()
->class('btn')
->hxPost('/counter')
->text('+1'));

Get Started

Install berry/html and render your first correct HTML string. Start here.

Writing Components

Split your UI into plain PHP functions or component classes. Write components.

Extensions

Add your own fluent methods, just like the HTMX integration does. Build extensions.

Static Analysis

Keep more of your view code visible to

PHPStan

and your editor. Use PHPStan.

Integrations

Use Berry with Symfony, HTMX, or your own application glue. See integrations.