piedweb / render-html-attributes
[Plates 和 Twig 扩展] 将数组转换为 HTML 标签属性。
v2.0.6
2023-12-04 11:39 UTC
Requires
- php: ^8.1
Requires (Dev)
- phpunit/phpunit: ^9
- twig/twig: ^3
- vimeo/psalm: ^4.3
README
本包是 Twig 和 Plate 引擎(已不支持,自 v1.0.3 版本起)的扩展。
Plates 自 v1.0.3 版本后不再受支持。
针对同一目标提供两个功能 通过对象/PHP 数组操作 HTML 标签属性
attr({class: "col", id: "piedweb", data-content:"Hello :)', ...})
将数组转换为 HTML 标签属性mergeAttr($attributes1, $attributes2, [$attributes3, ...])
合并多个数组而不丢失值(例如:['class' => 'main']
+['class' => 'content']
=['class' => 'main content']
)
目录
使用方法
对于 Twig
在 twig 中加载扩展(例如对于 symfony)
piedweb.twig.extension.render_attributes:
class: PiedWeb\RenderAttributes\TwigExtension
public: false
tags:
- { name: twig.extension }
然后使用它
{{ attr({class:"main content"})|raw }}
{{ mergeAttr({class:"main"}, {class:"content"})|raw }}
对于 Plates
/* Template Init */ $templateEngine = new \League\Plates\Engine('app/views'); /* Load this extension */ $templateEngine->loadExtension(new \PiedWeb\RenderAttributes\PlatesExtension()); $this->render('test', ['attributes' => ['class' => 'content']]);
在您的 app/views/test.php
模板文件中
<?php $defaultAttributes = ['class' => 'main']; $attributes = isset($attributes) ? $this->mergeAttr($defaultAttributes, $attributes) : $defaultAttributes; ?> <div<?=$this->attr($attributes)?>>Hello World !</div>
将渲染
<div class="main content">Hello World !</div>
安装
composer require piedweb/render-html-attributes
要求
独立扩展。
见 composer.json
文件。
贡献者
- 原作者 Robin (来自阿尔卑斯山的 PiedWeb)
- ...
许可证
MIT 许可证(详情见 LICENSE 文件)