Hi,
I created a Kirbytag for a progressbar but i have a little bit a problem with the attributes.
the Kirbytag should looks like (progressbar: value: 1235 max: 5615 unit: EUR class: blue)
.
My Problem now is that I have to declare the value-attribute directly after (progressbar:1235 β¦)
Is there a way to have a empty kirbytagname?
My Code
Kirby::plugin('kdjfs/progressbar', [
'tags' => [
'progressbar' => [
'attr' => [
'value',
'max',
'unit',
'class'
],
'html' => function($tag) {
$value = (int)str_replace('.', '', $tag->value);
$max = (int)str_replace('.', '', $tag->max);
if(is_numeric($value) && is_numeric($max)) {
$valuePercent = (100 * $value) / $max;
}
return
'<div class="progressbar-container">
<div class="progress">
<progress id="progressBar" role="progressbar" class="progress-bar w-100 ' . $tag->class . '" max="100" aria-valuemax="100" value="' . $valuePercent . '" aria-valuenow="' . $valuePercent . '" aria-valuemin="0" ></progress>
</div>
<ul class="progressbar__values d-flex justify-content-between pl-0">
<li class="progressbar__value-item mb-0">' . number_format($value, 0, ',', '.') . ' ' . $tag->unit . '</li>
<li class="progressbar__value-item">' . number_format($max, 0, ',', '.') . ' ' . $tag->unit . '</li>
</ul>
</div>';
}
]
]
]);
This example should work.
The main problem I see with your code is that $valuePercent
is not defined if the conditions are not true (so you should set a default value). And you can get a division by zero error if max is set to 0.
1 Like
Hi,
thanks for your improvments. But my startingproblem with the value-attribute exitst further.
here my actualised plugin code
<?php
Kirby::plugin('kdjfs/progressbar', [
'tags' => [
'progressbar' => [
'attr' => [
'value',
'max',
'unit',
'class'
],
'html' => function($tag) {
$valuePercent = 0;
$value = (int)str_replace('.', '', $tag->value);
$max = (int)str_replace('.', '', $tag->max);
if(is_numeric($value) && is_numeric($max) && $max > 0) {
$valuePercent = (100 * $value) / $max;
}
return
'<div class="progressbar-container">
<div class="progress">
<progress id="progressBar" role="progressbar" class="progress-bar w-100 ' . $tag->class . '" max="100" aria-valuemax="100" value="' . $valuePercent . '" aria-valuenow="' . $valuePercent . '" aria-valuemin="0" ></progress>
</div>
<ul class="progressbar__values d-flex justify-content-between pl-0">
<li class="progressbar__value-item mb-0">' . number_format($value, 0, ',', '.') . ' ' . $tag->unit . '</li>
<li class="progressbar__value-item">' . number_format($max, 0, ',', '.') . ' ' . $tag->unit . '</li>
</ul>
</div>';
}
]
]
]);
I think I donβt understand the problem. Of course a kirbytag needs a tagname, so you can either pass no argument to it like you do in this example (progressbar: value: 1235 max: 5615 unit: EUR class: blue)
or you pass your value to progressbar
as in (progressbar:1235 β¦)
. Could you tell us what your desired kirbytag should look like?
Hi,
it should like that: (progressbar: value: 1235 max: 5615 unit: EUR class: blue)
But it works in that way: (progressbar: 1235 max: 5615 unit: EUR class: blue)
.
Cheers
The problem seems to be related to the attribute being called value
. Try renaming it to something else, e.g. startvalue
.
1 Like
Hi,
yes I figured it also out before 5 min. :).
What also should avoid to use asattribute-name is title
it brings the same problems up.
Thanks @moonwalk and @texnixe