Hey,
I’m using the great plugin from @bnomei kirby3-srcset. But this plugin doesn’t allow to set a caption to the image. A good practice would be to wrap the image inside a figure
and a figcaption
for the caption text. How do you think this could be done with the new Html:: method ?
'tags' => [
'srcset' => [
// https://getkirby.com/docs/reference/text/kirbytags/image
'attr' => ['caption', /* The caption attr */ 'preset', 'lazy', 'prefix', 'class', 'imgclass', 'link', 'linkclass', 'target', 'rel', 'snippet'],
'html' => function ($tag) {
// TODO: move code to class
try {
$file = Kirby::instance()->file($tag->value, $tag->parent());
if ($file) {
$preset = (string) $tag->preset;
if (\Kirby\Toolkit\Str::contains($preset, ' ') || \Kirby\Toolkit\Str::contains($preset, ',')) {
$preset = str_replace(['[', ']', ',', ' ', 'px'], ['', '', ' ', ' ', ''], $preset);
$preset = array_map(function ($v) {
return trim($v);
}, explode(' ', $preset));
}
$prefix = (string) $tag->prefix;
$class = $tag->class ? trim($tag->class) : null;
$imgclass = $tag->imgclass ? trim($tag->imgclass) : null;
$snippet = $tag->snippet ? trim($tag->snippet) : 'plugin-srcset-img';
$srcset = \Bnomei\Srcset::srcset($file, $preset, boolval($tag->lazy), $prefix, $class, $imgclass, $snippet);
if ($tag->link) {
$attr = [
'href' => trim($tag->link),
];
if ($tag->linkclass) {
$attr['class'] = trim($tag->linkclass);
}
if ($tag->target) {
$attr['target'] = trim($tag->target);
}
if ($tag->rel) {
$attr['rel'] = trim($tag->rel);
}
// wrap $srcset in array to avoid encoding
// https://github.com/getkirby/kirby/blob/master/src/Toolkit/Html.php#L367
return \Kirby\Toolkit\Html::tag('a', [$srcset], $attr);
} else {
return $srcset . PHP_EOL;
}
}
return '';
} catch (Exception $ex) {
return $ex->getMessage();
}
},
],
],