# Reuse snippets in kirby

**URL:** <https://forum.getkirby.com/t/reuse-snippets-in-kirby/7035>\
**Category:** Questions\
**Tags:** v2\
**Created:** [March 22, 2017, 1:34pm UTC](https://forum.getkirby.com/t/reuse-snippets-in-kirby/7035 "2017-03-22T13:34:59Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![andalusi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/andalusi/32/1700_2.png) [@andalusi](https://forum.getkirby.com/u/andalusi)\
**Post date:** [March 22, 2017, 1:34pm UTC](https://forum.getkirby.com/t/reuse-snippets-in-kirby/7035/1 "2017-03-22T13:34:59Z")

</div>

My project grows every day, so I decide to put repeating content in a reusable snippet and call the snippet in place:

```
<?php if($image = $data->image()): ?>
	<img src="<?php echo thumb($image, array(
			'width' => 480,
			'quality' => 1
		))->url(); ?>"
		data-src="<?php echo $image->url() ?>"
		alt="<?php echo $site->title()->html(); ?> – <?php echo html($data->image()->caption()) ?>"
		class="lazyload blur-up" />
<?php endif ?>

```

than I call the snippet in a another snippet:

```
<?php snippet('image') ?>

```

But I got an error: `Undefined variable: data`

How do I send variables with a snippet?  
There is an section here: [https://getkirby.com/docs/templates/snippets](https://getkirby.com/docs/templates/snippets) but I did not get it to work ☹

---

<div class="post-metadata">

**Author:** ![carstengrimm](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/carstengrimm/32/188_2.png) [@carstengrimm](https://forum.getkirby.com/u/carstengrimm)\
**Post date:** [March 22, 2017, 1:41pm UTC](https://forum.getkirby.com/t/reuse-snippets-in-kirby/7035/2 "2017-03-22T13:41:16Z")

</div>

```auto
snippet('nameofsnippet', $variable, false/true);

```

the last parameter will “echo” or render the content and print it like it is, such as for in emails…

---

<div class="post-metadata">

**Author:** ![andalusi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/andalusi/32/1700_2.png) [@andalusi](https://forum.getkirby.com/u/andalusi)\
**Post date:** [March 22, 2017, 1:46pm UTC](https://forum.getkirby.com/t/reuse-snippets-in-kirby/7035/3 "2017-03-22T13:46:40Z")

</div>

@carstengrimm

```
<?php snippet('image', $data; true) ?>

```

Not sure what is wrong, but this gives the following error:

`syntax error, unexpected ';', expecting ',' or ')'`

---

<div class="post-metadata">

**Author:** ![carstengrimm](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/carstengrimm/32/188_2.png) [@carstengrimm](https://forum.getkirby.com/u/carstengrimm)\
**Post date:** [March 22, 2017, 1:47pm UTC](https://forum.getkirby.com/t/reuse-snippets-in-kirby/7035/4 "2017-03-22T13:47:05Z")

</div>

you use $data; instead $data,

(simicolon is wrong)

---

<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:** [March 22, 2017, 1:47pm UTC](https://forum.getkirby.com/t/reuse-snippets-in-kirby/7035/5 "2017-03-22T13:47:19Z")

</div>

```auto
<?php snippet('image', $data) ?>

```

---

<div class="post-metadata">

**Author:** ![andalusi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/andalusi/32/1700_2.png) [@andalusi](https://forum.getkirby.com/u/andalusi)\
**Post date:** [March 22, 2017, 1:48pm UTC](https://forum.getkirby.com/t/reuse-snippets-in-kirby/7035/6 "2017-03-22T13:48:29Z")

</div>

```
<?php snippet('image', $data) ?>

```

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

---

<div class="post-metadata">

**Author:** ![carstengrimm](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/carstengrimm/32/188_2.png) [@carstengrimm](https://forum.getkirby.com/u/carstengrimm)\
**Post date:** [March 22, 2017, 1:49pm UTC](https://forum.getkirby.com/t/reuse-snippets-in-kirby/7035/7 "2017-03-22T13:49:20Z")

</div>

did you define your variable $data somewhere? you can’t call a variable if it’s not available

---

<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:** [March 22, 2017, 1:49pm UTC](https://forum.getkirby.com/t/reuse-snippets-in-kirby/7035/8 "2017-03-22T13:49:38Z")

</div>

Then you probably have to use the long version:

```auto
<?php snippet('image', array('data' => $data)) ?>

```

provided that the variable used in the snippet, that references the snippet, is called $data.

---

<div class="post-metadata">

**Author:** ![andalusi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/andalusi/32/1700_2.png) [@andalusi](https://forum.getkirby.com/u/andalusi)\
**Post date:** [March 22, 2017, 1:51pm UTC](https://forum.getkirby.com/t/reuse-snippets-in-kirby/7035/9 "2017-03-22T13:51:00Z")

</div>

@carstengrimm @texnixe

The long form did the trick!

---

<div class="post-metadata">

**Author:** ![andalusi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/andalusi/32/1700_2.png) [@andalusi](https://forum.getkirby.com/u/andalusi)\
**Post date:** [March 22, 2017, 1:52pm UTC](https://forum.getkirby.com/t/reuse-snippets-in-kirby/7035/10 "2017-03-22T13:52:31Z")

</div>

It is possible to hand over more than one $variable?

---

<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:** [March 22, 2017, 1:52pm UTC](https://forum.getkirby.com/t/reuse-snippets-in-kirby/7035/11 "2017-03-22T13:52:55Z")

</div>

BTW: since you have already defined $image, you can then use this variable throughout in your snippet:

```auto
<?php echo html($image->caption()) ?>

```

---

<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:** [March 22, 2017, 1:53pm UTC](https://forum.getkirby.com/t/reuse-snippets-in-kirby/7035/12 "2017-03-22T13:53:34Z")

</div>

Yes,

```auto
<?php snippet('image', array('data' => $data, 'someothervariable' => $someothervariable)) ?>

```

Edit: you can also use another variable name in the snippet, for example:

```auto
<?php snippet('image', array('something' => $someother)) ?>

```

---

<div class="post-metadata">

**Author:** ![andalusi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/andalusi/32/1700_2.png) [@andalusi](https://forum.getkirby.com/u/andalusi)\
**Post date:** [March 22, 2017, 1:54pm UTC](https://forum.getkirby.com/t/reuse-snippets-in-kirby/7035/13 "2017-03-22T13:54:33Z")

</div>

Thanks, this is awesome! 🙂

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 4:50pm UTC](https://forum.getkirby.com/t/reuse-snippets-in-kirby/7035/14 "2025-02-06T16:50:24Z")

</div>


