# Embedding Snippets

**URL:** <https://forum.getkirby.com/t/embedding-snippets/900>\
**Category:** Questions\
**Tags:** v2\
**Created:** [April 25, 2015, 3:30pm UTC](https://forum.getkirby.com/t/embedding-snippets/900 "2015-04-25T15:30:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterbinks](https://avatars.discourse-cdn.com/v4/letter/p/50afbb/32.png) [@peterbinks](https://forum.getkirby.com/u/peterbinks)\
**Post date:** [April 25, 2015, 3:30pm UTC](https://forum.getkirby.com/t/embedding-snippets/900/1 "2015-04-25T15:30:41Z")

</div>

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](http://getkirby.com/forum/feedback/20140313/embed-snippet-inside-of-a-post)

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [April 26, 2015, 12:34pm UTC](https://forum.getkirby.com/t/embedding-snippets/900/2 "2015-04-26T12:34:25Z")

</div>

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. 😉

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 26, 2015, 4:07pm UTC](https://forum.getkirby.com/t/embedding-snippets/900/3 "2015-04-26T16:07:51Z")

</div>

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](http://getkirby.com/docs/advanced/kirbytext)

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 27, 2015, 5:15am UTC](https://forum.getkirby.com/t/embedding-snippets/900/4 "2015-04-27T05:15:23Z")

</div>

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

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

```php
<?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:

```php
(snippet: my-snippet)

```

---

<div class="post-metadata">

**Author:** ![sakamies](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/sakamies/32/645_2.png) [@sakamies](https://forum.getkirby.com/u/sakamies)\
**Post date:** [July 16, 2015, 4:56am UTC](https://forum.getkirby.com/t/embedding-snippets/900/5 "2015-07-16T04:56:21Z")

</div>

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);`

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [July 16, 2015, 11:43am UTC](https://forum.getkirby.com/t/embedding-snippets/900/6 "2015-07-16T11:43:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![andybraren](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/andybraren/32/382_2.png) [@andybraren](https://forum.getkirby.com/u/andybraren)\
**Post date:** [July 24, 2015, 1:52pm UTC](https://forum.getkirby.com/t/embedding-snippets/900/7 "2015-07-24T13:52:01Z")

</div>

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](http://getkirby.com/docs/templates/snippets#snippet-as-variable-value).

Thanks everyone, this is really useful.

---

<div class="post-metadata">

**Author:** ![sakamies](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/sakamies/32/645_2.png) [@sakamies](https://forum.getkirby.com/u/sakamies)\
**Post date:** [July 24, 2015, 4:53pm UTC](https://forum.getkirby.com/t/embedding-snippets/900/8 "2015-07-24T16:53:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kate](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/kate/32/5552_2.png) [@Kate](https://forum.getkirby.com/u/Kate)\
**Post date:** [February 3, 2016, 2:24pm UTC](https://forum.getkirby.com/t/embedding-snippets/900/9 "2016-02-03T14:24:08Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [February 3, 2016, 3:38pm UTC](https://forum.getkirby.com/t/embedding-snippets/900/10 "2016-02-03T15:38:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kate](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/kate/32/5552_2.png) [@Kate](https://forum.getkirby.com/u/Kate)\
**Post date:** [February 3, 2016, 5:08pm UTC](https://forum.getkirby.com/t/embedding-snippets/900/11 "2016-02-03T17:08:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [February 3, 2016, 5:28pm UTC](https://forum.getkirby.com/t/embedding-snippets/900/12 "2016-02-03T17:28:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kate](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/kate/32/5552_2.png) [@Kate](https://forum.getkirby.com/u/Kate)\
**Post date:** [February 3, 2016, 5:31pm UTC](https://forum.getkirby.com/t/embedding-snippets/900/13 "2016-02-03T17:31:54Z")

</div>

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](http://nils-fischer.net/veroeffentlichungen/test)

---

<div class="post-metadata">

**Author:** ![jensfranke](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jensfranke/32/722_2.png) [@jensfranke](https://forum.getkirby.com/u/jensfranke)\
**Post date:** [February 4, 2016, 11:48pm UTC](https://forum.getkirby.com/t/embedding-snippets/900/14 "2016-02-04T23:48:56Z")

</div>

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.

![](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/0/02533be4bd9ab027abbf25dcdc849bf3d5ec9948.png)

Form my point of view this issue is related to my issue: [Snippet in kirbytag output unnecessary p-tag](https://forum.getkirby.com/t/snippet-in-kirbytag-output-unnecessary-p-tag/3224/2)

---

<div class="post-metadata">

**Author:** ![thewebprojects](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thewebprojects/32/10107_2.png) [@thewebprojects](https://forum.getkirby.com/u/thewebprojects)\
**Post date:** [December 7, 2017, 8:43pm UTC](https://forum.getkirby.com/t/embedding-snippets/900/15 "2017-12-07T20:43:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [December 7, 2017, 8:56pm UTC](https://forum.getkirby.com/t/embedding-snippets/900/16 "2017-12-07T20:56:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![thewebprojects](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thewebprojects/32/10107_2.png) [@thewebprojects](https://forum.getkirby.com/u/thewebprojects)\
**Post date:** [December 7, 2017, 10:13pm UTC](https://forum.getkirby.com/t/embedding-snippets/900/18 "2017-12-07T22:13:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [December 7, 2017, 10:20pm UTC](https://forum.getkirby.com/t/embedding-snippets/900/19 "2017-12-07T22:20:21Z")

</div>

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:

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

```

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

---

<div class="post-metadata">

**Author:** ![thewebprojects](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thewebprojects/32/10107_2.png) [@thewebprojects](https://forum.getkirby.com/u/thewebprojects)\
**Post date:** [December 7, 2017, 10:26pm UTC](https://forum.getkirby.com/t/embedding-snippets/900/20 "2017-12-07T22:26:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [December 7, 2017, 10:28pm UTC](https://forum.getkirby.com/t/embedding-snippets/900/21 "2017-12-07T22:28:44Z")

</div>

This snippet worked as well:

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

```

Edit: Are you using any plugins?

[Next page](https://forum.getkirby.com/t/embedding-snippets/900.md?page=2)
