I’m trying to create a simple plugin to parse kirbytext and split the results into separately wrapped divs based on a certain symbol. I’m basing it off the Columns example but am getting a 500 error.
I’m pretty sure this is occurring when I try to parse the strings to apply regular kirbytext, but I can’t figure out why!
Aren’t you constructing an endless loop by calling kirbytext() from within a kirbytags hook? Every time you call kirbytext() within that hook, the hook is executed again…
Looking at the columns example from the Cookbook, it uses preg_replace_callback, i.e. executing the code manipulation – and calling kirbytext() – only for instances where the regex matches, while your code applies the manipulation to any content piped into kirbytext(). That’s where I’d suspect the eternal loop to stem from.
That’s a really good point. Sorry, I’m not very experienced with PHP, but any ideas how I could avoid that whilst also making sure that I’m manipulating the whole content piped into kirbytext()? I had a few passes at using preg_replace_callback with a regex expression matching everything, but had the same issue (predictably, I suppose).
only applies the cutting-into-columns routine on text wrapped between (columns) and (/columns) in the text. You’ll need some kind of check/filter to ensure that the calls to kirbytext() from within your function don’t trigger the same loop again.
The easiest would be to stick to the cookbook example where the text-to-be-divided is wrapped within such Kirbytags. Alternatively, you could try by introducing a filter to only do the “magic” after the regex actually split anything:
That way, your section-logic only applies if preg_split actually returns an array with more than one string. This may have performance implications, so I’d at least replace
Sebastian, thank you so much for your help here. That has solved the problem and really clarifies the root cause of it. I had a vague inkling about dealing with situations where this function would operate on content without this new separator, although I’m not sure I would have gotten there myself. Thank you.