Snippet in kirbytag output unnecessary p-tag

I try to use snippets inside a kirbytag to reuse some components. it works but I get some strange p-tags at the beginning and at the end of my container o-grid-wrap.

here a some screenshots of my kirbytag, my snippet and the output.

my kirbytag:

my snippet (the first </p> close the open kirbytext to create a new grid-container):

output with strange empty p-tags:

Moving one line in my snippet change the position of the wrong p-tag.

Any ideas?

Thanks in advance for your help!

First of all, could you please post your code as a code block (begin and end with three backticks). It is easier to read and correct.

And could you please post a part of your website´s source code, instead of the DOM-Tree, because sometimes the browser tries to fix tags and adds closing html tags itself.

First of all, could you please post your code as a code block (begin and end with three backticks). It is easier to read and correct.

I also know, but the support of the forum-editor for that is not very well:

<? kirbytext::$tags['textinset'] = array(
  'html' => function($tag) {

      $searchkey = $tag->attr('textinset');

      $module = $tag->page()->children()->findBy('title', $searchkey);
      
      $html =  snippet('components/test', array('title' => $module->title(), 'text' => $module->text()->kirbytext()), true);

      return $html;
  }
);

And could you please post a part of your website´s source code, instead of the DOM-Tree, because sometimes the browser tries to fix tags and adds closing html tags itself.

I know, but in this case the DOM-Tree has the same structure like the source-view and the screenshot is more readable. But here we go with the source-view:

</div></div></div><div class="o-grid-wrap u-clearfix"><div class="l1-l14"></p>
<h4>Test Titel</h4>
<p></div></p>
<div class="l5-l12">
    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. </p>
<p>Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.</p></div>
<p></div><div class="o-grid-wrap u-clearfix"><div class="l5-l12"><div class="c-standard-text c-font-style-f">

I don´t know, when exactly the kirbytags are executed. It seems to be, that the text is loaded from the file, all kirbytags are replaced/ parsed and then the markdown parser runs on that text.
This is my assumption, because every line break in your code is interpreted as the end of a paragraph, except the headings.
We should ask @lukasbestle or @texnixe about that.

Yes, that is exactly what is happening. This is because Kirbytags can return Markdown themselves, and that should be parsed.

The order is the following:

pre filters
tagsify
markdownify
smartypantsify
post filters

You can see how it is implemented in the Kirbytext class.

I agree that this is a bug though. Maybe the order should be changed.

Should I try to use the pre- & post-filter to close the old wrapper and open the new one?

I think the best approach would be to use a post filter instead of the Kirbytag. This post filter would then find tags with a different syntax (like {snippet: something}). This would avoid the interference with the Markdown parser.

@lukasbestle: Thank you so much for the hint!

Maybe someone is interested in my solution:

<? kirbytext::$post[] = function($kirbytext, $text) {

    $text = preg_replace_callback('!\(myKirbyTag:(.*?)\)!is', function($matches) use($kirbytext) {

    $searchkey = trim($matches[1]);

    $module = $kirbytext->field->page->children()->findBy('uid', $searchkey);

    $html = snippet('components/mySnippet', array('title' => $module->title(), 'text' => $module->text()->kirbytext()), true);

    return $html;

    }, $text);

  return $text;
};
1 Like

That would be perfect for a new topic in the “Solutions” category. Would you mind posting it there?

9 posts were split to a new topic: Inserting snippet in Kirbytext breaks if it contains UTF8 quotes

Strangely enough even when @jensfranke solution - i get everything wrapped in <p> tags.

Its a year old post so maybe something has changed?

I just want to create some block elements using kirby text tags. Strangely enough kirbytext::$tags['image'] (in core in kirby/extensions/tags.php) doesnt seem to have any special treatment yet it is not wrapperd in <p> when used (it is wrapped in <figure> but thats something generated in the tag).

What am i missing?