netzmacht / html
用于HTML渲染的简单库
3.0.0
2023-05-10 11:51 UTC
Requires
- php: ^8.1
Requires (Dev)
- doctrine/coding-standard: ^11.0.0
- phpcq/runner-bootstrap: ^1.0@dev
- phpspec/phpspec: ^7.0
This package is auto-updated.
Last update: 2024-09-04 18:54:09 UTC
README
此库是一个PHP辅助库,用于创建HTML输出。
安装
此扩展可以使用Composer安装
composer require netzmacht/html:^3.0
要求
PHP ^8.1
基本用法
在视图中定义属性
属性
$attributes = new Netzmacht\Html\Attributes(); $attributes ->setId('a_id') ->addClass('a_class') ->setAttribute('data-target', '#some');
此库使用魔术方法__toString
将辅助对象转换为字符串。现在输出变得非常简单
<div <?= $attributes; ?>><span class="label">Label</span> This is a paragraph.</div>
当然,您可以在生成之前更改属性
<div <?= $attributes->setId('second')->removeClass('a_class')->addClass('new_class'); ?>>the content</div>
元素
当然,您也可以创建整个元素。该库了解独立 HTML元素,这些元素不能有任何子元素,以及节点,这些节点有子元素。请注意,CSS类作为数组传递。
$factory = new Netzmacht\Html\Factory\ElementFactory(); $paragraph = $factory->create('p', array('id' => 'a_id', 'class' => array('description')); $label = $factory->create('span'); $label ->addClass('label') ->addChild('Label this'); $paragraph ->addChild('This is a paragraph.') ->addChild(' '); // add space between both elements ->addChild($label, Netzmacht\Html\Element\Node::POSITION_FIRST); // add at first position
现在您可以输出整个元素
<article> <?= $paragraph; ?> </article>