Download the PHP package fabianmichael/kirby-template-attributes without Composer
On this page you can find all versions of the php package fabianmichael/kirby-template-attributes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fabianmichael/kirby-template-attributes
More information about fabianmichael/kirby-template-attributes
Files in fabianmichael/kirby-template-attributes
Package kirby-template-attributes
Short Description Better HTML attribute handling for your snippets and templates.
License MIT
Homepage https://github.com/fabianmichael/kirby-template-attributes
Informations about the package kirby-template-attributes
Kirby Template Attributes
Better attribute API for snippets and templates
This plugin brings Vue.js/Laravel-Blade-like attribute composition to the templates of your Kirby project. This is an exploration in search of better HTML attribute handling for nested snippets and components.
[!NOTE] This plugin is completely free and published under the MIT license. However, if you are using it in a commercial project and want to help me keep up with maintenance, please consider to ❤️ sponsor me for securing the continued development of the plugin.
Requirements
- Kirby 4/5 (use version 1.x for Kirby 3 installations)
- PHP 8.3 (because of Enumeration support)
Installation
The recommended is installation via composer.
Alternatively, you can also download the plugin and install it manually by copying it to the site/plugins/ folder of your website.
Usage
Basic usage
Use the attributes() helper for generating a string of attributes:
You can also use named arguments if you prefer a leaner syntax. Be aware, that this only works as long as you don’t have dashes in your attribute names:
Or if all you have is an attributes string, you can also feed the that to the attributes() helper:
⚠️ If you need XML-compatible attributes, always call $attributes->toXml() instead of just echoing the Attributes object, because otherwise all attributes will be converted to lower-case.
In many cases, you need to set different classes. The classes() helper is a nice shortcut for improved readability:
The classes() helper is pretty flexible and also accepts multiple paramaters, each of those can eithe be a string or array (but please ensure to write readible code anyways):
Merging attributes
You can also use magic invoke method by calling the Attributes object like
a function. This is basically syntactic sugar for passing-down attributes
the snipept include chain:
This is the same as:
Output is:
Before/After
You can set $before and $after, just like for Kirby’s Html::attr() helper by using the corresponding methods:
Examples
A button component exists as a snippet in site/snippets/button.php:
A common situation would be the requirement to add attributes when calling the snippet('button') helper class, e.g. class, data-*, title, aria-* etc. Developers cannot handly every possible attribute for each component. The previous attributes() helper could help here:
This works better, but we still cannot extend the class attribute easily. Enter the new attributes() helper:
Even shorter:
This becomes even cooler, because the classes can be assigned conditionally as an array:
This is already cool and makes working with attributes for snippets much easier, e.g. is we use the button in site/snippets/menu.php:
Custom Snippet component
It makes a lot of sense to standardize the usage of this plugin across your project and to reduce boilerplate code. A custom snippet component can help with that by ensuring that the $attr variable is always available:
No longer do your snippets have to include something like $attr ??= [] at the top:
You can event take this a step further by invoking $attr() as a function, which ist a shortcut for attributes([…])->merge($attr):
License
MIT (but you are highly encouraged to ❤️ sponsor me, if this piece of software helps you to pay your bills).