Render a string with kirbytext using page's files

I want to render a page with its images, but before using kirbytext I need to transform all #hashtags in links (link: http:// text: hashtag). I have a problem with images that don’t render, 'cause I’m not sure how to pass the page’s data to kirbytext()

function taggize($str){
  $re = '/#([a-zA-Z0-9-_]+)/m';
  return preg_replace($re, "(link: items/tag/$1 text: #$1)", $str);
}

kirbytext(taggize($item->text()->value()), $item); // error: array expected

kirbytext(taggize($item->text()->value()), $item->toArray()); // no images are rendered

Thanks for any help!

kirbytext(taggize($item->text()->value()), [$item]);

Still not showing images :frowning:

Also, maybe you could update the docs with the right solution.

Do you mean images that are added as KirbyTags? And what exactly are you replacing here, there seems to be an issue with parentheses here. Could you post some example text?

Updated docs.

My page’s text in the panel looks like this:

(image: bitmap.jpg)
#ui #design

I want to turn all the hashtags that appear in the page’s text into links. For example in my output I want to automatically turn the string ‘#design’ into a link like this:

<a href="http://mysite.com/items/tag/design">#design</a>

And that works fine with the taggize function above.
But to do so I can’t use the kirbytext function directly on the field, hence the need to use the kirbytext() helper and passing the page’s data to render the images.

Hope it’s clear now

Works for me, but the issue is that you need to echo your stuff.

echo kirbytext(taggize($item->text()->value()), [$item]);

That’s not the issue, I am echoing it in my template but tue images don’t show up.
It seems the page’s data is not recognized.

// Page: items/cool-sites

$string = taggize($page->text()->value());
echo kirbytext($tring, [$page]);

// output html
<figure><img alt="" src="http://mysite.com/bitmap.jpg"></figure>
<a href="http://mysite.com/tag/ui">#ui</a>
<a href="http://mysite.com/tag/design">#design</a>

// expected output
<figure><img alt="" src="http://mysite.com/media/pages/items/cool-sites/3340574285-1555063277/bitmap.jpg"></figure>
…

If I use $page->text()->kirbytext() images render fine.

Have you tested this in a fresh Starterkit? Maybe something is interfering. Plugins?

What is your Kirby version?

Wouldn’t it make sense to use a custom KirbyTag for this purpose where you include the hashtags in the image tag:

(taggedimage: someimage tags: #ui, #design)

Then render that as figure tag with figcaption. Seem to be more semantically useful than links that are not tied to the image?

If these tags belong to the image, that is.

Actually they don’t. Images and tags are independent from one another.

Ok, I’ll try again on a fresh starterkit.