effectra/html-render

Effectra Html Render 包。

v1.0.0 2023-05-26 20:50 UTC

This package is auto-updated.

Last update: 2024-09-18 12:44:19 UTC


README

Effectra HTML Render 是一个 PHP 库,提供了一组用于以面向对象方式生成和操作 HTML 元素的类。

安装

您可以通过 Composer 安装此包。在您的终端中运行以下命令

composer require effectra/html-render

使用方法

创建 HTML 元素

HtmlElement 类允许您创建具有不同标签、内容和属性的 HTML 元素。以下是如何创建 HTML 元素的示例

use Effectra\HtmlRender\HtmlElement;

// Create an HTML element with a <div> tag and some content
$element = new HtmlElement('This is a div element', 'div');

// Get the content of the HTML element
$content = $element->getContent();
echo $content; // Output: This is a div element

// Get the tag name of the HTML element
$tag = $element->getTag();
echo $tag; // Output: div

// Get all attributes of the HTML element
$attributes = $element->getAttributes();
print_r($attributes); // Output: []

// Set the content of the HTML element and create a new instance
$newElement = $element->withContent('New content');

生成 HTML 标签

Render 类提供了生成常见 HTML 标签的静态方法。以下是一些示例

use Effectra\HtmlRender\Render;

// Generate the HTML <html> tag
$htmlTag = Render::tagHtml('Content', 'en');

// Generate the HTML <head> tag
$headTag = Render::tagHead('Head content');

// Generate the HTML <body> tag
$bodyTag = Render::tagBody('Body content');

// Generate the HTML <title> tag
$titleTag = Render::tagTitle('Page Title');

使用 HTML 元素工厂

HtmlElementFactory 类是创建 HtmlElement 类实例的工厂。以下是一个示例

use Effectra\HtmlRender\Factory\HtmlElementFactory;

$factory = new HtmlElementFactory();

// Create an HTML element using the factory
$element = $factory->createHtmlElement('Element content', 'span', ['class' => 'highlight']);

// Get the content of the created element
$content = $element->getContent();
echo $content; // Output: Element content

贡献

欢迎贡献!如果您发现任何问题或对改进有建议,请打开一个问题或提交一个拉取请求。

许可证

此包是开源的,并使用 MIT 许可证授权。有关更多信息,请参阅 LICENSE 文件。