hananils/kirby-attrs-helper
Composer 安装命令:
composer require hananils/kirby-attrs-helper
包简介
Setting attributes on elements in snippets and templates quickly becomes convoluted. This gets worse if attributes require additional logic, like conditionally adding segmented values like classnames or datasets. Attributes helper mitigates this issue by offering a simple interface to set attributes
README 文档
README
Setting attributes on elements in snippets and templates quickly becomes convoluted. This gets worse if attributes require additional logic, like conditionally adding segmented values like classnames or datasets. Attributes helper mitigates this issue by offering a simple interface to set attributes.
Note
Please check out the online documentation at kirby.hananils.de/plugins/attrs-helper for further information.
Usage
Attributes helper takes an associative array with the keys specifying the attribute names and the values specifying the corresponding attribute values. Values can either be static if passed as string or conditional if passed as array.
Static values
If values are passed as string, the plugin will create an attribute for each key using the given value.
<p
<?= attrs([
'id' => 'introduction',
'class' => 'highlighted'
])?>
>
An introductory paragraph.
</p>
This will create the following markup:
<p id="introduction" class="highlighted">An introductory paragraph.</p>
Conditional values
The attribute values can also be constructed conditionally:
<p
<?=attrs([
'id' => $page->isHomePage() ? 'home' : false
])?>
>
A paragraph.
</p>
This will only add the id attribute if the current page is the homepage. Otherwise, the attribute will be omitted.
Boolean values
If values evaluate to a boolean value, an attribute is only rendered if its value is true. In the following case, the checked attribute will only be added if the page is listed.
<input
<?=attrs([
'type' => 'checkbox',
'checked' => $page->isListed()
])?>
>
The result will either be a checked or unchecked checkbox:
<!-- listed --> <input type="checkbox" checked /> <!-- unlisted, draft --> <input type="checkbox" />
Segmented values
If the value itself is an array, all keys will be considered value segments that will be joined by as separator (a space by default). The associated array value is used as the condition to determine whether a segment should be added to the attribute or skipped.
This is useful if you'd like to set classnames or datasets dynamically.
The following example will either create a class attribute with the value published has-image, published, has-image or none at all depending on the page settings:
<article
<?=attrs([
'class' => [
'published' => $page->published()->isTrue(),
'has-image' => $page->images()->isNotEmpty()
]
])?>
>
An example article.
</article>
The helper also allows you to combine static and dynamic segments:
<article
<?=attrs([
'class' => [
'some', 'static', 'classnames',
'published' => $page->published()->isTrue(),
'has-image' => $page->images()->isNotEmpty()
]
])?>
>
An example article.
</article>
In this case, some static classnames will always be added to the class attribute.
Custom delimiter
If you'd like to join segmented values with a custom delimiter, you can pass it as second argument to the attrs function:
<article
<?=attrs([
'data-list' => [
'some', 'static', 'values'
]
], '|')?>
>
An example article.
</article>
This example will return some|static|values as value for the data-list attribute.
Installation
Download
Download and copy this repository to /site/plugins/attrs-helper.
Git submodule
git submodule add https://github.com/hananils/kirby-attrs-helper.git site/plugins/attrs-helper
Composer
composer require hananils/kirby-attrs-helper
License
This plugin is provided freely under the MIT license by hana+nils · Büro für Gestaltung.
We create visual designs for digital and analog media.
hananils/kirby-attrs-helper 适用场景与选型建议
hananils/kirby-attrs-helper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 6, 最近一次更新时间为 2025 年 06 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 hananils/kirby-attrs-helper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hananils/kirby-attrs-helper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 32
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-25
