List block custom class to ul

Hi, is it possible to customize the default List block snippet, and add class to ul tag?

I don’t want to add a div around it

<?php /** @var \Kirby\Cms\Block $block */ ?>
<?= $block->text();
1 Like

Since the snippet only echoes what is stored in the content file, all you could do at this point is inject a class via string manipulation.

Hi, @texnixe, thank you very much for your answer. If I am not asking too much, could you, please, give me a code example, hint, or reference to something similar?

You need to do some regex voodoo, check out PHP’s preg_replace() function.

If you are just as bad at regex stuff like me, this website is a great helper: https://regex101.com/

It actually turned out to be a pretty easy solution.

<?php /** @var \Kirby\Cms\Block $block */ 

$list = $block->text();
$list = preg_replace('/<ul>/', '<ul class="custom-class">', $list);

?>
<?= $list;

Thanks!

P.S. And yeah, I’m bad with regex stuff too :man_shrugging:

4 Likes

Thanks, I was just looking for this!

One question remains: Is something like this possible to get the classes I set in the panel?

$list = preg_replace('/<ul>/', '<ul class="$block->class()">', $list)

Hey,

this should work:

$list = preg_replace('/<ul>/', '<ul class="' . $block->class() . '">', $list)
1 Like