I have a snippet for an article link. That snippet basically creates a box with the title, a content excerpt, and image of a page.
I want to be able to use that snippet inside a KirbyTag aswell. For example, (article: my-article-id) would render that snippet and use the given article for the page. Here’s how the tag looks:
The problem is that I have indentation in my snippet and the resulting markup also has it. Then, Kirby parses that markup and renders indented code as a code block with <pre> tags. This breaks the markup. How would I avoid that?
I just checked. In a recent project I did it like this to make snippets work in custom tags. Don’t remember the exact reasons any more but perhaps this helps.
'tags' => [
'slider' => [
'html' => function($tag) {
$files = new Files();
foreach (Str::split($tag->value) as $path) {
if ($file = $tag->file($path)) {
$files->append($file);
}
}
$string = snippet('kirbytags/slider', [
'images' => $files,
'style' => 'for-article',
], true);
// replace line breaks with spaces to silence the markdown parser
return str_replace(PHP_EOL, ' ', $string);
}
]
]
I think that removing the newlines makes it work. Whitespace plays an important role in code formatting for markdown. In the snippet that I posted above if I remove the empty new line, the code works.
The problem with that is if you have a <pre> tag in the snippet that you do want to appear, removing the newlines will effectively change the content. Removing newlines might have other side effects, I suppose. I think Kirby should handle this case itself so there are no surprises like that.
Or perhaps we should get a Html::stripWhitespace() method that removes whitespace between tags. However, parsing HTML is always a bad idea and I don’t think that would be the best option.