vdhicts / html-element
轻松从PHP创建HTML元素
v3.0.0
2022-03-06 11:13 UTC
Requires
- php: ^7.4|^8.0
Requires (Dev)
- phpstan/phpstan: ^1.2
- phpunit/phpunit: ^9.0
README
此包允许您轻松地从PHP创建HTML元素。它受到 David Walsh的博客文章 的启发,但进行了进一步改进。
use Vdhicts\HtmlElement\HtmlElement; // Create new html element with attribute name $selectElement = new HtmlElement('select'); $selectElement->setAttribute('name', 'something'); $selectElement->generate(); // Create new html element with text and attribute $paragraphElement = new HtmlElement('p', 'text', ['class' => 'center']);
要求
此包需要PHP 7.4或更高版本。
安装
您可以通过composer安装此包
composer require vdhicts/html-element
用法
可以使用以下方式创建一个新的HTML元素
use Vdhicts\HtmlElement\HtmlElement; $containerElement = new HtmlElement('div'); $containerElement->setAttribute('class', 'container');
元素可以包含文本
$containerElement->setText('Hello World');
元素可以有一个或多个属性
// One attribute $containerElement->setAttribute('class', 'container'); // Multiple attributes $containerElement->setAttributes(['class' => 'container', 'role' => 'container']);
或者元素可以包含另一个HTML元素
$colElement = new HtmlElement('div', '', ['class' => 'col-md-6']); $rowElement = new HtmlElement('div', '', ['class' => 'row']); $rowElement->inject($colElement); $containerElement->inject($rowElement);
文本和属性也可以添加,这将与现有的文本或属性合并。
方法
getTag
或setTag
用于请求或更改标签。getAttributes
或getAttribute(attribute)
用于检索所有属性及其值或一个属性及其值setAttributes
或setAttribute(attribute, value)
用于操作所有属性或仅一个。addAttributeValue
向属性添加值。removeAttributes
或removeAttribute(attribute)
用于删除所有属性或仅一个。removeAttributeValue(attribute, value)
用于从一个属性中删除一个值。getText
、addText
或setText
用于检索、添加或设置内部文本。inject
用于将元素注入到当前元素中。generate
生成元素并返回其字符串表示形式。output
输出generate
的结果。
测试
单元测试位于tests
文件夹中。运行方法为
composer test
当您需要一个代码覆盖率报告时,该报告将在build/report
文件夹中生成。运行方法为
composer test-coverage
贡献
欢迎任何贡献,但应满足PSR-12标准,并且请为每个功能/错误创建一个pull request。作为交换,您将成为此页面的贡献者。
安全
如果您在此或其他Vdhicts包中发现任何安全相关的问题,请通过security@vdhicts.nl而不是使用问题跟踪器发送电子邮件。
支持
如果您遇到与此解析器相关的问题或对此有任何疑问,请随时在GitHub上打开一个问题。
许可证
此包是开源软件,根据MIT许可证许可。
关于Vdhicts
Vdhicts 是我个人的公司名称,我为该公司作为自由职业者工作。Vdhicts为企业和教育机构开发并实施IT解决方案。