Snippet returning <pre> tags

I’m currently using the same method the starter kit uses to add the gallery to to a textarea. For example, {{ gallery }}. But I’m using it to add a form to a page with {{ form }}. Until today it’s worked pretty well. My only issue is now I’m checking for some values and skipping areas if the value is empty which seems to leave blank areas which are triggering <pre> tags so I get this…

Is there a work around for ensuring that a snippet ignores white space and never returns <pre> tags?

(Note: the snippet return parameter is set to true.)

This is already marked as solved?

No, it isn’t solved yet. It was late when I posted it and accidentally chose the wrong tag. Any ideas about solving this issue would be super helpful.

Could you show us what you have in your snippet?

Form Snippet

<form name="<?= $form->form_name() ?>" method="post">
    <div class="<?=$form->slug() ?> Form">
        <input type="hidden" name="uid" value="<?= $form->uid() ?>">
        <div id="<?= $form->form_id() ?>" class="Form--container">
            <?php foreach($form->content()->form()->toBuilderBlocks() as $block): ?>
                <?php snippet('blocks/' . $block->_key(), array('data' => $block)) ?>
            <?php endforeach ?>
            <?php
                $hook_ids = array();
                $hooks = "";
                foreach($form->content()->zaps()->toBuilderBlocks() as $zap) {
                    $hook_segments = Url::path($zap->hook()->value());
                    $segments = explode('/', $hook_segments);
                    array_push( $hook_ids, end($segments));
                }
                $hooks = implode(" ", $hook_ids);
            ?>
        </div>
        <?php if(!empty($hooks)): ?>
            <input type="hidden" name="hooks" value="<?= $hooks ?>" />
        <?php endif ?>
    </div>
</form>

Hook in Plugin

'hooks' => [
        'kirbytags:after' => function ($text, $data, $options) {

            if ($page = $data['parent']->forms()->toPage()) {
                $form = snippet('form', ['form' => $page], true);
            } else {
                $form = '';
            }

            return str_replace('{{ form }}', $form, $text);
        }
    ]

And what in that snippet did you add that caused it to break, the hook logic stuff? I’d put that at the top of the file anyway, better yet into a function or page method/model.

Honestly, I’m not sure why it’s failing now. :man_shrugging: My guess is anytime there’s a space in the HTML it forces a <pre> tag.

I’d test if it is one of the block snippets or the main form that is causing the issue first, by uncommenting the snippet call first and adding or removing parts of the main form.

@texnixe Here’s a simple test I did with a paragraph in the same snippet…

Without space:

<form name="<?= $form->form_name() ?>" method="post">
    <p>Testing</p>
</form>


<form name="<?= $form->form_name() ?>" method="post">

    <p>Testing</p>
</form>

With Space:

No empty lines, then…

But I’m confused why a snippet would ever return anything with <pre> tags around it.

I think the problem is that you used the kirbytags:after hook. Markdown is parsed after it. The empty line and indentation make it think it’s a code block. Try using the kirbytext:after hook.

It’s just my guess. I haven’t used these hooks before.

I agree with @pedroborges. Try kirbytext:after instead.

Ok, If I try kirbytext:after I don’t get access to the $data parameter, only $text. How am I able to get the form from there to replace the {{ form }} tag?

It doesn’t seem to happen if you use only 2 spaces for indentation instead of 4.

@bastianallgeier The Starterkit example uses kirbytags:after as well. Wonder if we should change this then?

You can access the current page inside the hook with the page() function.

1 Like

Thanks! That resolved my issue. I appreciate all of the help figuring that out. :slight_smile:

Hi,
I’ve got the same <pre>-tag problem, but instead of a placeholder,
I use a custom kirbytag.

Kirby::plugin('my/custom-tags', [
  'tags' => [

    'gallery' => [
      'html' => function($tag) {
            
          $tagName = $tag->attr('gallery');
          $tags = $tag->parent()->children();
          
          foreach($tags as $tag){
              if($tag->title()->toString() === $tagName){
                $gallery = snippet('album', ['data' => $tag], true);
              } 
          }
          return $gallery;
      }
    ]
  ]
]);

If I load the snippet without the “true” paramater, the album gets rendered - but above the kirbytext and when I reduce the indentation to two spaces it actually works, but it makes my code hard to read.
Are You still thinking about changing this?

I was referring to the code in the getkirby.com website.

The problem is the markdown parser that causes some issues in some place. I completely agree that snippets with no indents are completely annoying, and if you usually use four spaces, then having to use two spaces for these snippets is also a pain in the neck.

Ok, thanks for the explanation.
I think I can live with it. Just have to remind myself, when sth is not working as expected.
Took me quite a while to figure out what the problem in this case was :upside_down_face: