one234ru / html-tag-generator
用于生成HTML标签代码的PHP工具。
v1.0.3
2024-05-19 16:46 UTC
Requires
- php: >=7.1
README
基于配置数组的HTML标签源代码生成
安装
Composer
composer require one234ru/html-tag-generator:dev-main
手动
该库没有依赖项。只需HTMLtagGenerator.php文件即可工作。
require_once 'HTMLtagGenerator.php';
使用方法
通过将配置数组传递给构造函数来创建对象。然后,将对象视为字符串以获取HTML代码
$config = [ ... ]; $html_tag = new \One234ru\HTMLtagGenerator($config); echo $html_tag;
配置字段说明
tag
和 text
tag
是标签的名称,默认为div
。
text
将插入到打开标签之后。
示例
配置
[ 'text' => 'Here is some text' ]
结果
<div>Here is some text</div>
配置
[ 'tag' => 'span', 'text' => 'Here is some text' ]
结果
<span>Here is some text</span>
无关闭部分的标签(如input
)将自动检测并适当处理。
attr
attr
是键值对形式的属性列表。
所有值都使用htmlspecialchars()
进行编码。
如果值是数组,它将被转换为JSON。
[ 'text' => 'Here is some text', 'attr' => [ 'id' => 'main', 'class' => 'someclass', 'style' => 'font-weight: bold', 'data-something' => [ 'x' => 1, 'y' => 2 ] ] ]
结果(格式化以提高可读性)
<div id="main" class="someclass" style="font-weight: bold" data-something="{&quot;x&quot;:1,&quot;y&quot;:2}" > Here is some text </div>
某些属性,如checked
,以特殊方式处理:如果值转换为布尔值true
,则只有它们的名称将进入最终的HTML,否则什么都不用。
[ 'tag' => 'input', 'attr' => [ 'type' => 'checkbox', 'checked' => true ] ]
<input type="checkbox" checked>
[ 'tag' => 'input', 'attr' => [ 'type' => 'checkbox', 'checked' => false ] ]
<input type="checkbox">
这是为了消除来自HTML标准的模糊特性,这导致<input checked="">
或<input checked=0>
最终以与<input checked>
相同的方式工作,即属性的值不影响任何内容,即使它是false
或类似的内容,唯一排除属性的方法是完全排除它。
children
children
字段用于列出类似配置的子元素。
它们的源代码将插入到text
之后。
[ 'text' => 'Did you like it?', 'children' => [ [ 'tag' => 'input', 'attr' => [ 'type' => 'submit', 'value' => 'Yes!', // Да! ], ], [ 'tag' => 'input', 'attr' => [ 'type' => 'reset', 'value' => 'No', // Нет ] ], [ 'tag' => 'button', 'text' => "I don't know", // Не знаю ] ] ]
<div> Did you like it? <input type="submit" value="Yes!"> <input type="reset" value="No"> <button>I don't know</button> </div>
您可以直接传递HTML字符串而不是数组
[ 'tag' => 'ul', 'children' => [ '<li>One</li>', '<li>Two</li>', '<li>Three</li>', ] ]
<ul> <li>One</li> <li>Two</li> <li>Three</li> </ul>
不需要将内容包装在标签中
[ 'children' => [ 'This is the text at the beginning. ', '<b>This is a bold text.</b> ', 'This is the text at the end.', ] ]
<div>This is the text at the beginning. <b>This is a bold text.</b> This is the text at the end.</div>
实际上,以下两种配置产生相同的结果
[ 'text' => 'Here is some text' ]
[ 'children' => [ 'Here is some text' ] ]
<div>Here is some text</div>
如果传递字符串作为配置,它将用作最终HTML;当传递字符串作为子元素时,将利用这一事实。以下两种配置是等效的
[ 'text' => 'Here is some text' ]
'<div>Here is some text</div>'
结果
<div>Here is some text</div>
继承:规范化配置(normalizeConfig()
)
当扩展类时,您可以定义受保护的normalizeConfig()
方法,这允许使用非标准结构的配置。
例如,我们经常使用class
属性,并希望在数组的最顶层定义它,而不是在attr
中。在这种情况下,我们需要创建一个子类
class Test extends \One234ru\HTMLtagGenerator { protected function normalizeConfig($config) { if (isset($config['class'])) { $config['attr']['class'] = $config['class']; unset($config['class']); } return $config; } } echo new Test([ 'class' => 'something', ]); // Same thing using the basic class: echo new \One234ru\HTMLtagGenerator([ 'attr' => [ 'class' => 'something' ] ]);
结果
<div class="something"></div>