one234ru/html-tag-generator
Composer 安装命令:
composer require one234ru/html-tag-generator
包简介
PHP tool for generating code of HTML tags.
README 文档
README
HTML tags source code generation based on configuration array
Installation
Composer
composer require one234ru/html-tag-generator:dev-main
Manual
The library doesn't have any dependencies. Only HTMLtagGenerator.php file is required for work.
require_once 'HTMLtagGenerator.php';
Usage
Create the object passing configuration array to the constructor. Then get an HTML code treating the object as a string:
$config = [ ... ]; $html_tag = new \One234ru\HTMLtagGenerator($config); echo $html_tag;
Description of configuration fields
tag and text
tag is the tag's name, div by default.
text will be inserted right after opening tag.
Examples
Configuration:
[
'text' => 'Here is some text'
]
Result:
<div>Here is some text</div>
Configuration:
[
'tag' => 'span',
'text' => 'Here is some text'
]
Result:
<span>Here is some text</span>
Tags with no closing part (like input) will be detected automatically and processed
appropriately.
attr
attr is a list of attributes in a form of key-value pairs.
All the values are encoded using htmlspecialchars().
If the value is an array, it will be turned into JSON.
[
'text' => 'Here is some text',
'attr' => [
'id' => 'main',
'class' => 'someclass',
'style' => 'font-weight: bold',
'data-something' => [ 'x' => 1, 'y' => 2 ]
]
]
Result (formatted for readability):
<div id="main" class="someclass" style="font-weight: bold" data-something="{&quot;x&quot;:1,&quot;y&quot;:2}" > Here is some text </div>
Some attributes, like checked, are treated in a special way:
if the value converts to boolean true, only their names go to final HTML,
otherwise nothing goes anywhere:
[
'tag' => 'input',
'attr' => [
'type' => 'checkbox',
'checked' => true
]
]
<input type="checkbox" checked>
[
'tag' => 'input',
'attr' => [
'type' => 'checkbox',
'checked' => false
]
]
<input type="checkbox">
This is done for disambiguation of unobvious feature deriving from HTML standard, which
leads to <input checked=""> or
<input checked=0> eventually working the same way as <input checked>, i.e. the actual
value of the attribute not affecting anything even if it is false or something like that,
and the only way to discard the attribute is to exclude it completely.
children
The children field serves for listing of children elements in the form of similar
configurations.
Their source code will be inserted after 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>
You can pass HTML as a string directly instead of an array:
[
'tag' => 'ul',
'children' => [
'<li>One</li>',
'<li>Two</li>',
'<li>Three</li>',
]
]
<ul> <li>One</li> <li>Two</li> <li>Three</li> </ul>
It is not necessary to wrap the contents into tags:
[
'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>
Actually, the following two configurations yield the same result:
[
'text' => 'Here is some text'
]
[
'children' => [
'Here is some text'
]
]
<div>Here is some text</div>
If a string is passed as a configuration, it will be used as final HTML;
this fact is utilized when children are passed as strings. The following two configurations
are equivalent:
[
'text' => 'Here is some text'
]
'<div>Here is some text</div>'
Result:
<div>Here is some text</div>
Inheritance: normalizing configuration (normalizeConfig())
When extending the class, you may define protected normalizeConfig() method,
which allows using configurations of non-standard structure.
Let's say, we often use class attribute and would like to define it at the highest level of
array, not inside the attr. In this case we need to create a child class:
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' ] ]);
Result:
<div class="something"></div>
one234ru/html-tag-generator 适用场景与选型建议
one234ru/html-tag-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 05 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 one234ru/html-tag-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 one234ru/html-tag-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 27
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2024-05-19