Way to Use Parenthesis inside Kirbytag?

So wanting a way to escape parenthesis with a backslash, and not finding a solution for it that actually works, I went with this:

<?php

kirbytext::$pre[] = function($kirbytext, $value) {
    return str_replace('\(','[[',str_replace('\)',']]',$value));
};
kirbytext::$post[] = function($kirbytext, $value) {
    return str_replace(']]',')',str_replace('[[','(',$value));
};

I saved this in plugins and called it escape.php. It’s a little hacky, but what it does is takes your escaped parentheses and changes them to something kirbytext will leave alone, then after kirbytext processes it switches them to regular parentheses.

You can always change the [[ and ]] to something even less likely to show up in your content. The beauty of this approach is that the user doesn’t have to learn what the alternate character is. The user only needs to know that to escape something you put a backslash in front of it, which is something a lot of users will already know, and many people will use as their first approach when trying to figure out how to get it to work.

1 Like