PHP code example of fabianmichael / kirby-template-attributes
1. Go to this page and download the library: Download fabianmichael/kirby-template-attributes library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
fabianmichael / kirby-template-attributes example snippets
<button <?= attributes([
'role' => 'button',
'aria-expanded' => 'false',
])
<img <?= attributes(
class: 'icon',
width: 16,
height: 16,
src: $image->url(),
alt: 'The funniest donkey ever!',
)
// get image dimensions as height="yyy" width="xxx"
$src = 'img.png';
$size = getimagesize($src)[3];
<button <?= classes([
'button',
'button--red' => $color === 'red', // class will only appear in class attribute, if condition is true
])
<button <?= classes('button', [
'button--red' => $color === 'red',
], 'absolute', 'top-0 left-0')
# site/snippets/button.php
<button <?= attributes([
'class' => 'button',
'role' => 'button',
'aria-expanded' => 'false',
'style' => '--foo: bar',
])->merge($attr ?? [])
// some parameter passed to the base button snippet for
// overriding the button type
$attr = attributes([
'type' => 'submit'
]);
$attr = attributes([
'class' => 'submit'
]);
<button class="button"><?= html($text ?? 'Button text')
<button <?= attributes($attr ?? [])
<button <?= attributes([
'class' => 'button',
])->merge($attr ?? [])
<button <?= classes('button')->merge($attr ?? [])
$text ??= 'Button text';
$size ??= 'normal';
$theme ??= null;
$attr ??= [];
<nav class="menu">
[…]
snippet('button', [
'text' => 'Toggle Menu',
'attr' => [
'class' => 'menu__button',
'aria-controls' => 'menu-popup',
'aria-expanded' => false,
'role' => 'teapot', // overrrides the default attribute
],
])
use Kirby\Cms\App;
use Kirby\Template\Snippet;
App::plugin('my/site', [
'components' => [
'snippet' => function (
App $kirby,
string $name,
array $data = [],
bool $slots = false
): Snippet|string {
// ensure that `$attr` is always available
$data['attr'] = attributes($data['attr'] ?? []);
return $kirby->nativeComponent('snippet')(
$kirby,
$name,
$data,
$slots
);
},
],
]);
# site/snippets/components/button.php
<button <?= attributes([
'class' => 'button',
])->merge($attr)
# site/snippets/components/button.php
<button <?= $attr([
'class' => 'button',
])