Embedding Snippets

I think this is what I am looking for to embed a little link menu for social media, displayed with Font Awesome icons, like this

(snippet: social)

I have a simple blueprint with a title and a textarea. What happens with the snippet tag is that the content of the snippet does not appear in the middle of the textarea where I placed it - it appears on top of it, below the heading that was created by the title field. Do I misunderstand the way the tag works?

No, but there was still a bug in the syntax apparently. Could you please try the code posted above again?

Ooops: what I get now is the raw code, as if the code is wrapped in pre-tags… Hm. Is it possible that it has to do with my PHP version? The provider is still on PHP 5.3, so I use Kirby 2.1.

Raw code as in “raw PHP code”? What does your HTML source code look like?

1 Like

Raw HTML code. Yes, it is exactly as I thought: the code is wrapped in pre- and code-tags:
An example is here: http://nils-fischer.net/veroeffentlichungen/test

I had the same issue like @Kate.

In my case the indentation force the problem with a pre & code-tag around my snippet-code if I call the snippet from a kirbytag.

Form my point of view this issue is related to my issue: Snippet in kirbytag output unnecessary p-tag

1 Like

Hello, I’m using this custom tag. See code below.

kirbytext::$tags['snippet'] = array(
  'attr' => array(
  ),
  'html' => function($tag) {
  	$file =  $tag->attr('snippet');
  	return snippet($file, array(), true);
  }
);

It seems like anything after the custom tag does not get parsed as markdown? For example…

## Some Header

Some paragraph of text.

(snippet: list-team)

And this is the last sentence in the textarea.

In the above case when I output this with kt() it does not wrap the final sentence with p tags. Everything above the snippet embed tag gets converted from markdown/kt to html.

Any insight?

What is in your snippet? What Kirby version? I can’t reproduce this in a Starterkit, I’m afraid…

Thanks for the reply. I’m working with version 2.57.

It’s definitely something with the snippet file itself. I narrowed it down and see that this behavior is due to the use of the kt() or kirbytext() in the snippet file that gets embedded.

Do you mean, due to using kt() within your snippet? For obvious reasons, I can’t test your snippet, at least not without having to recreate your structure, but if I create a simple snippet like this:

<div><?= $page->intro()->kt() ?></div>

and insert it via the snippet tag in the home page of a Starterkit, it works fine.

Hmmm… Right. When I remove all code from my snippet and simply use the following…

<?php #$site->title()->kt() ?>

Everything seems fine. I don’t know. Maybe it has to do with it cycling through through the structure object with the foreach loop. Don’t see why though.

This snippet worked as well:

<div>
  <?php foreach(page('blog')->children() as $child): ?>
    <h1><?= $child->title() ?></h1>
    <?= $child->text()->kt() ?>
  <?php endforeach ?>
</div>

Edit: Are you using any plugins?

Yes. Imageshrink and Focus

Does it make any difference if you remove those plugins? Can’t see how those would interfere, though.

Edit: I just saw something else: the original snippet you posted, is not wrapped in a container, but should be. Could you try that, please?

Good eye. I wrapped my snippet code in a DIV and it worked. I wouldn’t have thought of that. I’ll have to chalk that up as a rule to remember.

Thanks for your help!!!

FYI: The HTML in a Kirbytag definition has to be wrapped in a container as well.

Does this snippet tag need to be revised to work in Kirby v3?

The snippet itself: no; the way you create your custom kirbytag: yes.

/site/plugins/custom-tags/index.php

<?php
Kirby::plugin('yourname/custom-tags', [

    'tags' => [
        'snippet' => [
            'html' => function($tag) {
                return snippet('snippetname');
            }
        ]
    ]
]);

Or with dynamic snippet names

<?php
Kirby::plugin('yourname/custom-tags', [

    'tags' => [
        'snippet' => [
            'html' => function($tag) {
                $snippetName = $tag->value();
                return snippet($snippetName);
            }
        ]
    ]
]);
1 Like

@texnixe Is there something further that one needs to do to make the V3 plug-in above (dynamic snipped names) besides install it to the plug-in folder? Despite installation (plugins>custom-tags>custom-tags.php), my snippet tags are not replaced.

Example:

Version: (snippet: version-snippet)

… where version-snippet.php is in the snippets folder.

Even after installing the plug-in, this is rendered on the live HTML page as:

Version: (snippet: version-snippet)

There was a typo, now corrected, if you just copied it as is, maybe that is the issue.