studiow/html

生成HTML5标签的辅助包

v1.0.1 2015-10-29 23:11 UTC

This package is auto-updated.

Last update: 2024-09-24 19:44:57 UTC


README

生成HTML5标签的辅助包

用法

安装

安装此包最简单的方式是使用composer

composer require studiow/html

基本用法

使用此包生成HTML标签非常简单

// Create the element
$link = new \Studiow\HTML\Element("a", "Documents", ["href"=>"/documents"]);

// Output
echo (string) $link;
// prints <a href="/documents">Documents</a>

获取和设置属性

处理属性也很简单

// Create the element
$link = new \Studiow\HTML\Element("a", "Documents", ["href"=>"/documents"]);

// Set an attribute
$link->setAttribute('title', 'Go to documents');

// Get the value for an attribute
$link->getAttribute('title');  // returns  'Go to documents'
$link->getAttribute('attr_not_set');  // returns  null

// Remove an attribute
$link->removeAttribute('title');

添加类

处理类的工作方式与您预期的一样

// Create the element
$link = new \Studiow\HTML\Element("a", "Documents", ["href"=>"/documents"]);

// Add a single class
$link->addClass("button");

// Add multiple classes seperated by a space
$link->addClass("button button-documents");

// Remove a class
$link->removeClass("button");

// Check if the element has a certain class
$link->hasClass("button-documents");

方法链式调用

您可以继续将方法链式调用在一起

$link = new Studiow\HTML\Element("a");
$link->setInnerHTML("Documents")
        ->setAttribute("href", "/documents")
        ->addClass("button")
        ->addClass("button-documents")
        ->setAttribute('title', "Go to documents");
echo (string) $link; // Outputs <a href="/documents" class="button button-documents" title="Go to documents">Documents</a>

已知问题

大小写(不)敏感

HTML5属性应该是大小写不敏感的,但在这里它们是大小写敏感的。建议使用小写!

getChildren()等在哪里?

此包绝不是作为一个生成大量HTML的工具。虽然您可以将Element作为另一个Element的innerHTML使用,但当你这样做时,它将被转换为文本。

如果您发现自己正在PHP脚本中渲染大量HTML,您可能最好使用模板系统。