HTML output from snippet inside KirbyTag?

I want a kirbytag that I can use like this:

(block: my-items content: my-field)

Which, when used in a textarea, for example, will replace the tag with content from the my-items snippet and will pass the value of this page’s my-field field to it. I’ve made it like this:

Kirby::plugin('oblik/ikarov', [
  'tags' => [
    'block' => [
      'attr' => [
        'content'
      ],
      'html' => function ($tag) {
        $markup = snippet(
          'blocks/' . $tag->value,
          [
            'field' => page()->{$tag->content}()
          ],
          true
        );

        return $markup;
      }
    ]
  ]
]);

This correctly calls the snippet and passes the field value to it. However, in the snippet, I use whitespace to structure the code. When it’s later output in the template, the Markdown parser puts <pre> tags because it thinks the markup is a code block (due to the whitespace).

Instead of using $field->kirbytext() to output the textarea content, I tried with $field->markdown()->kirbytags(). This solved the problem with the <pre> tags, but now there were mystery <p> tags all over the place.

My next solution was to simply remove the whitespace in the tag function before returning the markup:

return preg_replace('/\s+/', ' ', $markup);

This appears to work perfectly, but I still feel like it’s a bad solution open to issues. Is there a better way to handle this problem?


I don’t know how kirbytext() works internally, but perhaps the whole kirbytag parsing should be part of the custom Markdown parser? I mean, it should process the tags and then do no more alterations to whatever they turned into, without conflicting with other Markdown rules.

You had already posted this issue here: How to render a snippet inside a custom KirbyTag without `<pre>` tags appearing? and I had thought you had created a ticket on GitHub but I can’t find it.

1 Like

I totally forgot about that. Didn’t found it while searching the forum too because I had formatted the title with different wording…

I once tested a custom markdown parser (can’t remember which one it was). With that parser the issue didn’t happen.

1 Like

I created an issue.