codehiggs / html
This package is auto-updated.
Last update: 2024-07-16 10:24:49 UTC
README
HTML
Descripción
这是一个PHP库,用于处理HTML标签的生成、属性和内容。它是安全的、高效的且易于使用。
要求
- PHP 8.0或更高版本
- Composer 2.6用于常规使用。
安装
composer require Higgs/Html
使用
<?php include 'vendor/autoload.php'; // Meta object. $meta = \Higgs\Html\HtmlTag::tag('meta', ['name' => 'author']); $meta->attr('content', 'pol dellaiera'); // Title object. $title = \Higgs\Html\HtmlTag::tag('h1', ['class' => 'title'], 'Welcome to HTMLTag'); // Paragraph object. $paragraph = \Higgs\Html\HtmlTag::tag('p', ['class' => 'section']); $paragraph->attr('class')->append('paragraph'); $paragraph->content('This library helps you create HTML.'); // Simple footer $footer = \Higgs\Html\HtmlTag::tag('footer', [], 'Thanks for using it!'); // Body tag. // Add content that can be transformed into strings. $body = \Higgs\Html\HtmlTag::tag('body', [], [$title, $paragraph, $footer]); // Fix something that was already added. $paragraph->attr('class')->remove('section')->replace('paragraph', 'description'); // Alter the values of a specific attributes. $meta->attr('content')->alter( function ($values) { return array_map('strtoupper', $values); } ); echo $meta . $body;
将打印
<meta content="POL DELLAIERA" name="author"/> <body> <h1 class="title">Welcome to HTMLTag</h1> <p class="description">This library helps you create HTML.</p> <footer>Thanks for using it!</footer> </body>
HTML构建器
该库包含一个HTML Builder类,允许您快速创建HTML内容。
<?php include 'vendor/autoload.php'; $builder = new \Higgs\Html\HtmlBuilder(); $html = $builder ->c(' Comment 1 ') // Add a comment ->p(['class' => ['paragraph']], 'some content') ->div(['class' => 'container'], 'this is a simple div') ->_() // End tag <div> ->c(' Comment 2 ') ->region([], 'region with <unsafe> tags') ->_() ->c(' Comment 3 ') ->a() ->c(' Comment 4 ') ->span(['class' => 'link'], 'Link content') ->_() ->div(['class' => 'Unsecure "classes"'], 'Unsecure <a href="#">content</a>') ->_() ->c(' Comment 5 '); echo $html;
这将产生
<!-- Comment 1 --> <p class="paragraph"> some content <div class="container"> this is a simple div </div> </p> <!-- Comment 2 --> <region> region with <unsafe> tags </region> <!-- Comment 3 --> <a> <!-- Comment 4 --> <span class="link"> Link content </span> </a> <div class="Unsecure "classes""> Unsecure <a href="#">content</a> </div> <!-- Comment 5 -->
技术说明
标签分析
The tag name An attribute The content
| | |
++-+ +-----+-----+ +----+-----+
| | | | | |
<body class="content node" id="node-123" data-clickable>Hello world!</body>
| |
+-----------------------+-----------------------+
|
The attributes
该库围绕3个对象构建。
- 处理属性、标签名称和内容的标签对象,
- 处理属性的属性对象,
- 处理由名称及其值组成的属性的属性对象。
标签对象使用属性对象,基本上是属性对象的存储。您可以单独使用这些对象中的任何一个。
所有方法都通过接口进行文档化,并且当需要时,您的IDE应该能够自动完成。
大多数方法参数是可变参数,可以接受无限嵌套的值或值的数组。您还可以链式调用大多数方法。
允许的值类型几乎是任何东西。如果它是一个对象,它必须实现`__toString()`方法。
示例
方法链
<?php include 'vendor/autoload.php'; $tag = \Higgs\Html\HtmlTag::tag('body'); $tag ->attr('class', ['FRONT', ['NODE', ['sidebra']], 'node', ' a', ' b ', [' c']]) ->replace('sidebra', 'sidebar') ->alter( function ($values) { $values = array_map('strtolower', $values); $values = array_unique($values); $values = array_map('trim', $values); natcasesort($values); return $values; } ); $tag->content('Hello world'); echo $tag; // <body class="a b c front node sidebar">Hello world</body>
以下所有示例都将生成相同的HTML。
<?php include 'vendor/autoload.php'; $button = html()::tag('button'); $button->attr('class', 'tabs-nav-link nav-link' . ($data['active'] ? ' active' : '')); $button->attr('id', $data['id'] . '-tab'); $button->attr('data-bs-toggle', 'tab'); $button->attr('data-bs-target', '#' . $data['id']); $button->attr('type', 'button'); $button->attr('role', 'tab'); $button->attr('aria-controls', $data['id']); $button->attr('aria-selected', $data['active'] ? 'true' : 'false'); $button->content($data['text']); echo($button->render()); $tag = \Higgs\Html\HtmlTag::tag('body'); $tag->attr('class', ['front', ['node', ['sidebar']]]); $tag->content('Hello world'); echo $tag; // <body class="front node sidebar">Hello world</body>
<?php include 'vendor/autoload.php'; $tag = \Higgs\Html\HtmlTag::tag('body'); $tag->attr('class', 'front', 'node', 'sidebar'); $tag->content('Hello world'); echo $tag; // <body class="front node sidebar">Hello world</body>
<?php include 'vendor/autoload.php'; $tag = \Higgs\Html\HtmlTag::tag('body'); $tag->attr('class', ['front', 'node', 'sidebar']); $tag->content('Hello world'); echo $tag; // <body class="front node sidebar">Hello world</body>
<?php include 'vendor/autoload.php'; $tag = \Higgs\Html\HtmlTag::tag('body'); $tag->attr('class', 'front node sidebar'); $tag->content('Hello world'); echo $tag; // <body class="front node sidebar">Hello world</body>
标签对象
<?php include 'vendor/autoload.php'; $tag = \Higgs\Html\HtmlTag::tag('body'); $tag->attr('class', 'front'); $tag->content('Hello world'); echo $tag; // <body class="front">Hello world</body>
属性对象
<?php include 'vendor/autoload.php'; $attributes = \Higgs\Html\HtmlTag::attributes(); $attributes->append('class', 'a', 'b', 'c'); $attributes->append('id', 'htmltag'); // Hence the trailing space before the class attribute. echo $attributes; // class="a b c" id="htmltag"
属性对象
<?php include 'vendor/autoload.php'; $attribute = \Higgs\Html\HtmlTag::attribute('class', 'section'); echo $attribute; // class="section"
依赖注入和扩展
感谢库中提供的工厂,可以使用不同的类来替换默认类。
例如:您想对“class”属性进行特殊处理。
<?php include 'vendor/autoload.php'; class MyCustomAttributeClass extends \Higgs\Html\Attribute\Attribute { /** * {@inheritdoc} */ protected function preprocess(array $values, array $context = []) { // Remove duplicated values. $values = array_unique($values); // Trim values. $values = array_map('trim', $values); // Convert to lower case $values = array_map('strtolower', $values); // Sort values. natcasesort($values); return $values; } } \Higgs\Html\Attribute\AttributeFactory::$registry['class'] = MyCustomAttributeClass::class; $tag = HtmlTag::tag('p'); // Add a class attribute and some values. $tag->attr('class', 'E', 'C', ['A', 'B'], 'D', 'A', ' F '); // Add a random attribute and the same values. $tag->attr('data-original', 'e', 'c', ['a', 'b'], 'd', 'a', ' f '); echo $tag; // <p class="a b c d e f" data-original="e c a b d a f "/>
相同的机制也适用于`Tag`类。
安全
为了避免安全问题,所有打印的对象都将进行转义。
如果对象用作输入并且实现了`__toString()`方法,则将它们转换为字符串。确保它们打印**不安全**的输出,以便它们不会被转义两次的责任在于用户。
代码质量、测试和基准测试
每次向库中引入更改时,Travis CI都会运行测试和基准测试。
该库使用PHPSpec编写了测试。请随意检查`spec`目录中的它们。运行`composer phpspec`以触发测试。
PHPBench用于基准测试库,要运行基准测试:`composer bench`
PHPInfection用于确保您的代码得到适当的测试,运行`composer infection`以测试您的代码。
贡献
请随意通过发送GitHub拉取请求来为此库做出贡献。我相当积极:-)