Custom Kirbytag Breaks Markdown after it

I have made a custom Kirby tag (my first one, yay!) which doesn’t quite work, it renders the tag but any markdown after the tag breaks and gets outputted as raw markdown instead of being turned into HTML.

My custom tag contains this:

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

    $html  = '<h2>' . $tag->attr('slideshow') . '</h2>';
    $html .= '<div class="swiper-container">';
    $html .= '<div class="swiper-wrapper">';
    foreach($tag->page()->mediagallery()->toStructure() as $image) {
      $html .= '<div class="swiper-slide" style="background-image: url(' . $image->url() . ')"></div>';
    }
    $html .= '</div>';
    $html .= '</div>';
    return $html;

  }
);

I have tried enabling markdown extra in the config but that just makes the whole thing dissappear. I’ve also tried adding the markdown="1" flag.

Try wrapping everything into a div.

Well thanks, that fixed it! But why? I like to know these things :slight_smile:

I can’t remember, there are a couple of posts around with this problem, somehow it only works properly if everything is wrapped in a single div.

I did look around the forum but couldn’t find much on the cause, but did find how turn on Markdown Extra in the process so not all bad.

Hmmm… This is happening to me again. Wrapping it doesn’t seem to work. Here’s my custom tag…

<?php
kirbytext::$tags['members'] = array(
	'html' => function($tag) {
		$group = $tag->attr('members');
		// Get members from current page and filter them by group
		$members = $tag->page()->members()->toStructure()->filterBy('member_group', $group);
		if($members->count() > 0 ): 
			$html = '<div class="container-max">';
			$html .= '<section>';
			$html .= '<h2>' . $group . '</h2>'; $html .= "\n"; 
			$html .= '<div class="grid grid-3-cols">'; 
			foreach($members as $member):
				$html .= '<article class="grid-item mbr-card">'; 
	            if($member->member_name()->isNotEmpty()): 
	            	$html .= '<h3 class="mbr-h">' . $member->member_name()->html() . '</h3>';
	            endif;
	            if($member->member_county()->isNotEmpty()):
	            	$html .= '<div class="mbr-cnty">' . $member->member_county() . '</div>';  
	            endif;                      
	            $html .= '</article>'; $html .= "\n"; 
			endforeach;
			$html .= '</div>';
			$html .= '</section>';
			$html .= '</div>';  
		endif;
		return $html;
	}
);

Just to be sure what needs to be wrapped in a div? I have a field named text and I’m using the following in my template.

<div class="body"><?= $page->text()->kt() ?></div>

That looks ok to me. What result do you get?

Don’t forget to define your $html variable right at the beginning:

$html = '';

Otherwise you get an unwanted result if your condition is not true.

Is the tag on a line by itself? not inside some other stuff? when i had the issue i think lack of white space kind of effected it. having a clear distinction between the tag and the stuff that comes before and after it seemed to change things a bit. Maybe your tags getting mangled in with the next bit of content or something. i seem to remember mine rendering ok if it was the very last thing in the text field… but it was 6 months ago, cant quite remember :slight_smile:

That shouldn’t make a difference, actually.

The problem with your tag was that you used a heading tag outside of a div container. That doesn’t work.

@thewebprojects has everything wrapped within a container element, though.

Got it.

It did not like those line breaks (below)

$html .= "\n";

I removed those and it’s good.

Ah, good to know. It didn’t do any harm in my test but I wondered what they were doing there. When your tag has a lot of html, consider using a separate template file.

What the heck? I included the line break code back in to see if it would break and research a bit more in an effort to confirm my findings. Now, even with the line breaks, it’s fine.

I’m afraid this will all be just confusing to someone with the same issue.

I’ll have to assume it was some cache issue - a remnant from some previous foul that persisted, then was eventually flushed.

Thank you two for your feedback.

@thewebprojects Thanks for reporting back! Honestly, I don’t know why you need those breaks, they only introduce empty paragraphs for nothing :thinking: ?

Well it was an attempt to clean up the formatting of the html it spit out. It’s really not needed.