定制 keruyphp/keruy-html 二次开发

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

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

keruyphp/keruy-html

最新稳定版本:v1.1.0

Composer 安装命令:

composer require keruyphp/keruy-html

包简介

Flexible fluent PHP DSL for HTML.

README 文档

README

Simple fluent PHP DSL for building HTML.

Installation

composer require keruyphp/keruy-html

Quick start

<?php

declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

use KeruyPHP\KeruyHtml\Html;

echo Html::div(['class' => 'box'], 'Hello world');

Passing content

You can build children in a few different ways.

1. As an array

The first array argument is treated as attributes. Pass an empty attribute array if you want to send children as an array.

echo Html::ul([], [
	Html::li('One'),
	Html::li('Two'),
	Html::li('Three'),
]);

2. With a closure

echo Html::ul(function () {
	Html::li('One');
	Html::li('Two');
	Html::li('Three');
});

3. As trailing arguments

echo Html::ul(
	Html::li('One'),
	Html::li('Two'),
	Html::li('Three'),
);

Output styles

You can render the same structure using an array or a closure.

Array-based output

echo Html::div(['class' => 'card'], [
	Html::h2('Title'),
	Html::p('Text from array children.'),
]);

Closure-based output

echo Html::div(function () {
	Html::h2('Title');
	Html::p('Text from closure children.');
});

Core methods

append()

append() is the universal child API. It accepts strings, nodes, arrays, generators and closures.

String values are treated as text and escaped automatically.

echo Html::div(function (\KeruyPHP\KeruyHtml\Tags\Tag $div) {
	$div->append(
		'Hello ',
		Html::strong('world'),
		'!',
	);
});

text()

text() is the explicit text helper. It is a semantic alias for appending escaped string content.

echo Html::p(function (\KeruyPHP\KeruyHtml\Tags\Tag $p) {
	$p->text('Price: ', '100 UAH');
});

raw()

raw() appends trusted HTML fragments without escaping.

echo Html::div(function (\KeruyPHP\KeruyHtml\Tags\Tag $div) {
	$div->raw('<strong>Trusted HTML</strong>');
});

Use raw() only for HTML you already trust.

Setting attributes and classes

You can add attributes and classes in three common ways.

1. In the attribute array

echo Html::div([
	'class' => 'box',
	'id' => 'main',
], 'Hello');

2. Inside a closure

echo Html::div(function (\KeruyPHP\KeruyHtml\Tags\Tag $div) {
	$div->class('box');
	$div->prop('id', 'main');

	Html::span('Hello');
});

3. At the end of the chain

echo Html::div('Hello')
	->class('box')
	->prop('id', 'main');

class()

The class() method is the main shortcut for CSS classes.

echo Html::div('Hello')
	->class('box')
	->class('has-background-light');

prop()

Use prop() for any attribute.

echo Html::a('Profile')
	->prop('href', '/profile')
	->prop('target', '_blank');

You can also combine it with regular attribute arrays:

echo Html::input(['type' => 'text'])
	->prop('name', 'email')
	->prop('placeholder', 'Email');

Pretty output

Call pretty() if you want formatted HTML instead of minified output.

echo Html::div(['class' => 'card'], function () {
	Html::h2('Title');
	Html::p('Formatted output example.');
})->pretty();

Notes

  • Strings are escaped automatically.
  • Use HtmlElement::raw() or HtmlUtil::raw() if you need raw HTML.
  • Use HtmlElement::text() or HtmlUtil::text() if you want explicit escaped text.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0
  • 更新时间: 2026-05-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固