gin0115/elmishphp-html
Composer 安装命令:
composer require gin0115/elmishphp-html
包简介
A functional based library for creating HTML in PHP, heavily inspired by the ELM HTML package
关键字:
README 文档
README
A functional library for creating HTML in PHP, heavily inspired by Elm's Html package. Each element is a typed value object with __toString — compose them with curried functions, render by stringification.
Why
Erm, next question........ok fine, why not. I really enjoyed ELMs approach to creating HTML and have played around with this idea before (Functional WP Plugin).
Install
composer require gin0115/elmishphp-html
Then the usual require 'vendor/autoload.php' and you're good to go.
How it works
Each HTML tag is a curried function. The first call passes attributes, the second passes children. Every function returns a typed object that knows how to render itself.
use function Gin0115\ElmishPHP\HTML\div; use function Gin0115\ElmishPHP\HTML\span; use function Gin0115\ElmishPHP\HTML\p; use function Gin0115\ElmishPHP\HTML\text; echo div(['id' => 'wrap', 'class' => 'card'])( p()(text('Hello, '), span(['class' => 'name'])(text('world'))), );
<div id="wrap" class="card"><p>Hello, <span class="name">world</span></p></div>
Elm-style formatting
For longer trees, leading-comma style mirrors elm-format:
div ([ 'id' => 'wrap', 'class' => 'card' ]) ( p () ( text('Hello, ') , span ([ 'class' => 'name' ])(text('world')) ) );
Text content
| escapes? | use for | |
|---|---|---|
text('...') |
yes | the default for any string |
raw('<b>x</b>') |
no | pre-rendered HTML you trust |
bare 'string' |
yes (auto) | shorthand for text('string') as a child |
echo div()(text('<script>alert(1)</script>')); // <div><script>alert(1)</script></div> echo div()(raw('<b>bold</b>')); // <div><b>bold</b></div>
Attributes
Attributes are an associative array. Three forms:
div([ 'id' => 'foo', // standard key=value — value is HTML-escaped 'data-flag' => null, // null value → bare flag attribute 'data-other', // positional entry → bare flag attribute ])(text('hi'));
<div id="foo" data-flag data-other>hi</div>
All attribute values are HTML-escaped. Keys are not (they're under your control).
Void elements
Void elements take attributes only — no second call for children:
echo br(); // <br> echo img(['src' => 'logo.png', 'alt' => 'logo']); // <img src="logo.png" alt="logo"> echo input(['type' => 'text', 'name' => 'q', 'required']); // <input type="text" name="q" required>
The void set: br, hr, img, input, wbr, col, source, track.
Custom tags via node()
For anything not in the built-in set:
use function Gin0115\ElmishPHP\HTML\node; echo node('custom-element', ['data-x' => 'y'])(text('whatever')); // <custom-element data-x="y">whatever</custom-element>
Type hierarchy
Everything renderable shares a small interface tree — useful for categorisation and type-narrowing in your own code:
Renderable extends \Stringable
├── Element
│ ├── BlockElement div, p, h1-h6, ul, blockquote, ...
│ ├── InlineElement span, a, strong, em, br, code, ...
│ ├── SectioningElement extends BlockElement header, footer, nav, main, ...
│ ├── FormElement form, input, button, select, ...
│ ├── TableElement table, tr, td, th, ...
│ ├── MediaElement img, video, iframe, ...
│ ├── InteractiveElement details, summary, dialog
│ └── VoidElement marker on br, hr, img, input, ...
└── TextNode Text, Raw
Every element function returns its concrete typed class (e.g. div(...)(...) returns Gin0115\ElmishPHP\HTML\Element\Div), so you can instanceof BlockElement or pass them around with full type info.
Supported tags
77 standard HTML elements out of the box.
| Category | Tags |
|---|---|
| Block | div, p, h1–h6, pre, blockquote, ul, ol, li, dl, dt, dd, figure, figcaption, hr |
| Inline | span, a, strong, em, small, b, i, u, mark, code, kbd, samp, sub, sup, time, abbr, cite, q, br, wbr |
| Sectioning | header, footer, main, nav, section, article, aside |
| Form | form, fieldset, legend, label, button, select, optgroup, option, textarea, input |
| Table | table, caption, colgroup, thead, tbody, tfoot, tr, td, th, col |
| Media | img, iframe, video, audio, canvas, picture, source, track |
| Interactive | details, summary, dialog |
For anything else, use node('tag-name', ...).
Tests
PHPUnit (unit / behaviour):
composer test
Playwright (browser-driven E2E against the kitchen-sink fixtures):
npm install npm run server:up # docker container on http://localhost:57893 npm run test:e2e -- # extra playwright flags after -- npm run server:down # when finished
The kitchen-sink fixture at tests/e2e/views/kitchen-sink.php exercises every category — visit http://localhost:57893/?fixture=kitchen-sink while the server is up.
Requirements
- PHP 8.2+
- (optional) Docker + Node for the e2e suite
License
GPL-2.0-or-later
gin0115/elmishphp-html 适用场景与选型建议
gin0115/elmishphp-html 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「html」 「functional」 「fp」 「elm」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 gin0115/elmishphp-html 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gin0115/elmishphp-html 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 gin0115/elmishphp-html 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Connection 'Db' codeception module to 'Yii2' module database settings
Functional library for php with proper currying
Iteration tools for PHP
PHP port of charmbracelet/bubbletea — Elm-architecture TUI runtime.
A PHP library with partial functions suitable for the pipe operator
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 38
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2026-04-24