gin0115/elmishphp-html 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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.

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require GitHub contributors GitHub issues PHP — PHPUnit + PHPStan Level 8 E2E (Playwright) codecov

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>&lt;script&gt;alert(1)&lt;/script&gt;</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, h1h6, 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 gin0115/elmishphp-html 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 38
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2026-04-24