Often find myself needing to wrap content in kirbytext to do fancier stuff with css or js. This is simply a light abstraction of a div wrapper via kirbytags:
A couple of months ago I was in need of something like this and came up with a similar function, but with a different approach, as I know nothing about php.
<?php
kirbytext::$pre[] = function($kirbytext, $text) {
// 1. match text from (style: class-name) to (style)
$text = preg_replace_callback('!\(style:(.*?)\)(.*?)\(style\)!is', function($matches) use($kirbytext) {
// 2. select text in string after `: ` and save it in a variable
$style_class = preg_split('!\:\s(.*?)\)!', trim($matches[1]));
// 3. select text between parentheses and save it in a variable
$styled_texts = preg_split('!\(style:(.*?)\)(.*?)\(style\)!', $matches[2]);
$html = array();
// 4. parse text as kirbytext
foreach($styled_texts as $styled_text) {
$field = new Field($kirbytext->field->page, null, trim($styled_text));
$html[] = kirbytext($field);
}
// 5. return text
return '<div class="' . implode($style_class) . '">' . implode($html) . '</div>';
}, $text);
return $text;
};
Nice, yep accomplishing the same thing ultimately! Passing arrays into str_replace for multiple replace is one of those very handy, easily overlooked features.
Will likely extend my plugin to accept arrays for specificity. Maybe the api could look like:
Thats’ really nice, and I think in a way is taking a different approach to the need of structuring a text field than Kirby Builder plugin.
I personally prefer to write my texts on a dedicated text editor and then copy pasting them on the kirby panel. Being able to quickly drop in some more structure within the same text field—mostly for visual necessity or even as you suggest to make a simple slideshow— would be great.
You don’t really need a plugin as such, I use the following custom kirbytag myself, which can be tweaked to suit your needs. It works in both Kirby 2 and Kirby 3.
Good suggestions all around! (nice straightforward solve @jimbobrjames)
I suppose the still handy thing with the wrapper plugin is the auto data attributes, but that would also be easy to pull into any of the solutions just mentioned as well. That said, updating this for k3 is still a good idea but I haven’t been doing as much Kirby dev of late, and I tend to publish/update plugins as needs come up in projects I work on. Hopefully I’ll be able to get around to it soon, in the meantime feel free to fork or dig around the src yourself, it’s a pretty straightforward solution!