webfiori/ui
Composer 安装命令:
composer require webfiori/ui
包简介
A library that provides basic utilities for creating HTML documents.
README 文档
README
A PHP library for creating HTML documents and DOM manipulation with an object-oriented approach.
Motivations
PHP's built-in DOMDocument works but has friction for HTML generation:
- Verbose — Creating a styled element with text takes 4-5 lines. This library does it in one:
new HTMLNode('div', ['class' => 'x'])->text('hello') - No chaining —
DOMDocumentreturns void on most mutations. This library is fluent. - HTML5 issues —
DOMDocumentis XML-based (libxml2), throws warnings on HTML5 tags, and mishandles void elements - No templates — No built-in support for loading partial HTML with variable slots
- Limited output control —
saveHTML()gives little control over formatting, quoting, or self-closing tags
This library's sweet spot is generating HTML from PHP — pages, emails, components — where a builder pattern and template system give better DX than the W3C DOM API.
This is a DOM builder, not a text-based template engine. If you need template inheritance, compiled templates, or a dedicated syntax (like Blade or Twig), use those instead. Use this library when you want to construct and manipulate HTML programmatically — server-rendered components, email generation, PDF markup, or any case where the structure is driven by logic rather than a layout file.
Table of Contents
- Key Features
- Supported PHP Versions
- Installation
- Quick Start
- Usage
- API Reference
- Testing
- Contributing
- License
- Support
- Changelog
Key Features
- Object-Oriented DOM Creation — Build HTML elements using intuitive PHP classes
- Template System — Support for both HTML templates with slots and PHP templates
- Iterator Support — Traverse child nodes using foreach loops
- Type Safety — Full type hints and comprehensive PHPDoc documentation
- Security First — Built-in HTML entity escaping in attribute values
- XML Support — Generate both HTML and XML documents
- Standalone Renderer —
HtmlRendererwith per-instance config, safe for async contexts
Supported PHP Versions
| Build Status |
|---|
Installation
composer require webfiori/ui
Quick Start
<?php require_once 'vendor/autoload.php'; use WebFiori\Ui\HTMLDoc; $doc = new HTMLDoc(); $doc->getHeadNode()->setPageTitle('My First Page'); $doc->setLanguage('en'); $body = $doc->getBody(); $body->addChild('h1')->text('Welcome to WebFiori UI!'); $body->addChild('p')->text('Building HTML has never been easier.'); echo $doc;
Usage
HTML Document Creation
use WebFiori\Ui\HTMLDoc; $doc = new HTMLDoc(); $doc->getHeadNode()->setPageTitle('My Application'); $doc->setLanguage('en'); $head = $doc->getHeadNode(); $head->addMeta('description', 'A powerful web application'); $head->addCSS('styles/main.css'); $head->addJs('scripts/app.js'); $body = $doc->getBody(); $body->addChild('header')->addChild('h1')->text('My Application'); $body->addChild('main')->addChild('p')->text('Main content here.'); $body->addChild('footer')->addChild('p')->text('© 2024 My App'); echo $doc;
Working with Elements
use WebFiori\Ui\HTMLNode; // Create elements with attributes $div = new HTMLNode('div', ['id' => 'main', 'class' => 'container']); $div->addChild('p')->text('Hello World'); // Method chaining $div->setAttribute('data-role', 'content') ->setStyle(['padding' => '1rem']); // Iterate children foreach ($div as $child) { echo $child->getNodeName(); }
Forms and Input
$form = $body->form(['method' => 'post', 'action' => '/login']); $form->label('Username:'); $form->br(); $form->input('text', ['name' => 'username', 'required' => '']); $form->br(); $form->label('Password:'); $form->br(); $form->input('password', ['name' => 'password', 'required' => '']); $form->br(); $form->input('submit', ['value' => 'Login']);
Tables and Data
use WebFiori\Ui\HTMLTable; $table = new HTMLTable(3, 4); $table->getCell(0, 0)->text('Name'); $table->getCell(0, 1)->text('Email');
Template System
HTML templates with slots:
<!-- template.html --> <div class="card"> <h2>{{title}}</h2> <p>{{content}}</p> </div>
$card = HTMLNode::fromFileAsNode('template.html', [ 'title' => 'My Card', 'content' => 'Card body text' ]);
PHP templates with variables:
// template.php <ul> <?php foreach ($items as $item): ?> <li><?= htmlspecialchars($item) ?></li> <?php endforeach; ?> </ul>
$list = HTMLNode::fromFileAsNode('template.php', [ 'items' => ['Apple', 'Banana', 'Cherry'] ]);
API Reference
Core Classes
| Class | Description |
|---|---|
HTMLNode |
Foundation class for all HTML elements |
HTMLDoc |
Represents a complete HTML document |
HeadNode |
The HTML head section |
HTMLTable |
Table creation and manipulation |
HtmlRenderer |
Standalone renderer with per-instance config |
TemplateCompiler |
HTML/PHP template loading and compilation |
Key Methods (HTMLNode)
| Method | Description |
|---|---|
addChild($node, $attrs) |
Add a child element |
setAttribute($name, $val) |
Set an attribute |
text($text) |
Set text content |
toHTML($formatted) |
Render to HTML string |
toXML($formatted) |
Render to XML string |
fromFile($path, $vars) |
Load from template |
fromFileAsDocument($path, $vars) |
Load as HTMLDoc |
fromFileAsNode($path, $vars) |
Load as single node |
fromFileAsArray($path, $vars) |
Load as node array |
Testing
cd tests
php ../vendor/bin/phpunit
Contributing
We welcome contributions! Please see our Contributing Guide for details.
- Clone the repository
- Install dependencies:
composer install - Run tests:
cd tests && php ../vendor/bin/phpunit - Check code style:
composer fix-cs
License
This library is licensed under the MIT License. See the LICENSE file for details.
Support
If you encounter any issues, please open an issue on GitHub.
Changelog
See Releases for version history.
webfiori/ui 适用场景与选型建议
webfiori/ui 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 44.66k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2020 年 09 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「library」 「code」 「html」 「dom」 「webfiori」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webfiori/ui 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webfiori/ui 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webfiori/ui 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
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
HTML and form generation
Inbox pattern process implementation for your Laravel Applications
Promotional Codes Generator for Laravel 5.1
A modern HTML DOM parser for PHP using DOMDocument and Symfony CssSelector.
统计信息
- 总下载量: 44.66k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 8
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-09-14