Embedding Snippets

Is this still a good way of embedding snippets inside of pages/posts in kirby?

http://getkirby.com/forum/feedback/20140313/embed-snippet-inside-of-a-post

The proposed solutions should all still work. Simply use whatever looks most useful to your workflow and if there are any problems, don’t hesitate to ask. :wink:

Just keep in mind that - if you go for the Kirbytext version - the way Kirbytext extensions are created has changed with Kirby 2 http://getkirby.com/docs/advanced/kirbytext

In fact, embedding a snippet is as easy as this:

New Kirbytag “snippet” (to be saved as snippet.php in /site/tags):

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

To invoke the snippet in your text file, type:

(snippet: my-snippet)
1 Like

I’ve found that if you embed snippets like that, they always render just before the parsed kirbytext content. Not sure if i’m doing something wrong. Returning the snippet output as parsed html and then outputting the tag seems to fix it. Basically add an extra parameter of true when calling the snippet.

Edit: fixed the code.
return snippet($file, array(), true);

Thanks for posting that bug! You are right and what you posted is the correct solution. I’ve corrected the above code snippet by @texnixe.

For whatever reason return snippet($file, true) doesn’t work for me on Kirby 2.1, but return snippet($file, array(), true) does. I found the syntax here.

Thanks everyone, this is really useful.

Thanks for that, true as the second parameter wont’t work. :smile: The second parameter is for passing variables and the third parameter can be true to return the parsed snippet.

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?