hirasso/attr
Composer 安装命令:
composer require hirasso/attr
包简介
A tiny HTML attribute generator written in PHP. Great for projects using tailwindcss and Alpine.js 🎡
README 文档
README
A tiny HTML attribute generator written in PHP. Great for projects using tailwindcss and Alpine.js 🎡
Installation
composer require hirasso/attr
Usage
attr()
Define your attributes in an associative way:
<?php /** Example: render a button with custom classes and styles */ ?> <button <?= attr([ 'type' => 'button', 'class' => [ 'border border-current p-3', 'bg-white text-black' => !$isActive, 'bg-blue-600 text-white' => $isActive ], 'style' => [ '--active-color' => 'red' ], 'data-toggle' => true ]) ?>> Click Me! </button>
...and the attr function transforms them into normal HTML:
<button type="button" class="border border-current p-3 bg-white text-black" style="--active-color: red;" data-toggle> Click Me! </button>
jsonAttr()
Render JSON so that it is safe to be used inside an HTMLElement attribute:
<?php /** Example: render an x-data attribute for Alpine.js */ ?> <div <?= attr([ 'x-data' => jsonAttr([ 'open' => true, "message" => "This 'quote' contains <b>bold text</b>" ]) ]) ?>> </div>
..the output will look like this and can be consumed by JavaScript:
<div x-data="{"open":true,"message":"This 'quote' contains <b>bold text<\/b>"}"></div>
Tailwind CSS IntelliSense
To get Tailwind CSS IntelliSense in VS Code working with attr(), add the following to your user settings or .vscode/settings.json:
{ "tailwindCSS.includeLanguages": { "php": "html" }, "tailwindCSS.experimental.classRegex": [ "/\\*tw\\*/\\s*'([^']*)'", // /*tw*/ hint, single-quoted "/\\*tw\\*/\\s*\"([^\"]*)\"", // /*tw*/ hint, double-quoted "->class\\(\\s*'([^']*)'", // fluent API, single-quoted "->class\\(\\s*\"([^\"]*)\"", // fluent API, double-quoted "'class'\\s*=>\\s*'([^']*)'", // plain string, single-quoted "'class'\\s*=>\\s*\"([^\"]*)\"", // plain string, double-quoted ["'class'\\s*=>\\s*\\[((?:[^\\[\\]]|\\[[^\\]]*\\])*)\\]", "'([^']*)'"], // conditional array, single-quoted ["'class'\\s*=>\\s*\\[((?:[^\\[\\]]|\\[[^\\]]*\\])*)\\]", "\"([^\"]*)\""], // conditional array, double-quoted ], }
This enables completions for all supported class syntaxes:
// Fluent API ->class('block p-3') ->class('bg-blue-600 text-white', $isActive) // Array class syntax (plain string) 'class' => 'block p-3' // Array class syntax (conditional) 'class' => [ 'block p-3', 'bg-blue-600 text-white' => $isActive, ] // Anywhere else, use the /*tw*/ hint comment $class = /*tw*/ 'text-white bg-blue-600';
Fluent API
For more expressive attribute building, use the fluent interface:
<button <?= attr() ->set('type', 'button') ->set('disabled', $isDisabled) ->class('border p-3') ->class('bg-blue-600 text-white', $isActive) ->class('bg-white text-black', !$isActive) ->style('--highlight', 'red') ->style('display', 'none', $isHidden) ->aria('expanded', 'false') ?>> Click Me! </button>
Available Methods
| Method | Description |
|---|---|
set($name, $value) |
Set any attribute. null and false values are ignored. |
class($class, $when) |
Add class(es). Supports space-separated classes. Optional condition. |
style($property, $value) |
Add inline style. null and false values are ignored. |
aria($name, $value, $when) |
Shortcut for aria attributes. |
merge($attributes) |
Merge an array of attributes (interop with existing code). |
The builder implements Stringable, so you can echo it directly or call ->toString().
Tip
More examples can be seen by browsing the test files.
Also, it's worth noting that both attr() as well as jsonAttr() escape all attributes for you. No need to do it yourself 🦦
hirasso/attr 适用场景与选型建议
hirasso/attr 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 837 次下载、GitHub Stars 达 5, 最近一次更新时间为 2024 年 07 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 hirasso/attr 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hirasso/attr 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 837
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-07-30