定制 webfiori/ui 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

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.

PHP 8.1+

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 chainingDOMDocument returns void on most mutations. This library is fluent.
  • HTML5 issuesDOMDocument is 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 controlsaveHTML() 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

  • 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 RendererHtmlRenderer with 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.

  1. Clone the repository
  2. Install dependencies: composer install
  3. Run tests: cd tests && php ../vendor/bin/phpunit
  4. 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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 44.66k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 8
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-09-14