# Failing to pass a variable to a snipet

**URL:** https://forum.getkirby.com/t/failing-to-pass-a-variable-to-a-snipet/13531
**Category:** Questions
**Tags:** v3
**Created:** [March 25, 2019, 10:50am UTC](https://forum.getkirby.com/t/failing-to-pass-a-variable-to-a-snipet/13531 "2019-03-25T10:50:37Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![j11k00](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@j11k00](https://forum.getkirby.com/u/j11k00)
#### Post date: [March 25, 2019, 10:50am UTC](https://forum.getkirby.com/t/failing-to-pass-a-variable-to-a-snipet/13531/1 "2019-03-25T10:50:37Z")

</div>

Found a lot of similar questions, but none of them seems to work in my case. I’m sure I’m overlooking something really simple, would be grateful if someone could point me in the right direction.

My snippet looks like this:

```
<?php foreach ($categories as $category): ?>
<section class="dishes">
  <h2><?= $category ?></h2>
  <?php foreach ($page->$category()->toStructure() as $dish): ?>
  <article class="dish">
    <h3 class="dish-name"><?= $dish->dish() ?></h3>
    <p class="dish-description"><?= $dish->description() ?></p>
    <p class="dish-price">€ <?= $dish->price() ?></p>
  </article>
  <?php endforeach ?>
</section>
<?php endforeach ?>

```

First tried without passing anything

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

Which gives me an error for the variable $category not being defined, so I figured passing the variable to the snippet would do it like so:

`<?php snippet('menu', ['category' => $category]) ?>`

But no luck ☹

---

<div class="post-metadata">

### Author: ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)
#### Post date: [March 25, 2019, 11:03am UTC](https://forum.getkirby.com/t/failing-to-pass-a-variable-to-a-snipet/13531/2 "2019-03-25T11:03:01Z")

</div>

The problem is this line i think…

```auto
<?php foreach ($page->$category()->toStructure() as $dish): ?>

```

Should probable be

```auto
<?php foreach ($category->nameofstucturefield()->toStructure() as $dish): ?>

```

You cant do `$category()`. That needs to be the name of the stucture field, and your also setting $category on the for each loop, which is what is coming out in your h2 tag, rather then what you passed through.

How are you setting `$categories`?

---

<div class="post-metadata">

### Author: ![j11k00](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@j11k00](https://forum.getkirby.com/u/j11k00)
#### Post date: [March 25, 2019, 11:13am UTC](https://forum.getkirby.com/t/failing-to-pass-a-variable-to-a-snipet/13531/3 "2019-03-25T11:13:17Z")

</div>

Hi, `$categories` is set in a controller like this:

```
<?php

return function () {

    $categories = [
        'Starters',
        'Pasta',
        'Meat',
        'Desert'
    ];

    return [
        'categories' => $categories
    ];

};

```

Everything works perfectly if in a page template, but moving it to a snippet doesn’t.

---

<div class="post-metadata">

### Author: ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)
#### Post date: [March 25, 2019, 11:22am UTC](https://forum.getkirby.com/t/failing-to-pass-a-variable-to-a-snipet/13531/4 "2019-03-25T11:22:29Z")

</div>

Ok so you have multiple structure fields on each page that match up with the `$categories` array set in the controller?

The names in the array should match up with the structure field names, which i think are case sensitive (i’ve run into case issues like this, even on different versions of linux).

Try this…

```auto
<?php foreach ($page->{$category}()->toStructure() as $dish): ?>

```

But i think there is probably a much better approach to doing this. I think i would probably collect up the contents of each field in the controller as an array to combine the structure fields under headings, and then loop through that in the snippet.

---

<div class="post-metadata">

### Author: ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)
#### Post date: [March 25, 2019, 11:38am UTC](https://forum.getkirby.com/t/failing-to-pass-a-variable-to-a-snipet/13531/5 "2019-03-25T11:38:18Z")

</div>

like @jimbobrjames said you need to use curly brackets to inline a variable into the method chain.

```auto
<?php foreach ($page->{$category}()->toStructure() as $dish): ?>

```

in snippet you say `<?php foreach ($categories` …  
so you need to do

```auto
$categories = ['a', 'b'];
snippet('menu', ['categories' => $categories]); // $categories not $category

```

or you can omit that and lazyload it in snippet like this

```auto
<?php if(!isset($categories)) { $categories = ['a', 'b']; }
<?php foreach ($categories as $category): ?>

```

---

<div class="post-metadata">

### Author: ![j11k00](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@j11k00](https://forum.getkirby.com/u/j11k00)
#### Post date: [March 25, 2019, 12:19pm UTC](https://forum.getkirby.com/t/failing-to-pass-a-variable-to-a-snipet/13531/6 "2019-03-25T12:19:42Z")

</div>

I’m afraid this is touching topics that are too advanced for my php knowledge ☹ 'm sure there is a better way of collecting the fields and looping them as @jimbobrjames suggested. I’ll see if I can find some tips on that. Anyway, I tried moving the `$categories` into the snippet and adding the curly brackets.

```
<?php $categories = ['Starters', 'Pasta', 'Meat', 'Desert']; ?>
<?php foreach ($categories as $category): ?>
<section class="dishes">
  <h2><?= $category ?></h2>
  <?php foreach ($page->{$category}()->toStructure() as $dish): ?>
  <article class="dish">
    <h3 class="dish-name"><?= $dish->dish() ?></h3>
    <p class="dish-description"><?= $dish->description() ?></p>
    <p class="dish-price">€ <?= $dish->price() ?></p>
  </article>
  <?php endforeach ?>
</section>
<?php endforeach ?>

```

Calling the snippet with:

`<?php snippet('menu', ['categories' => $categories]); ?>`

will still error out as Undefined variable: categories

---

<div class="post-metadata">

### Author: ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)
#### Post date: [March 25, 2019, 12:38pm UTC](https://forum.getkirby.com/t/failing-to-pass-a-variable-to-a-snipet/13531/7 "2019-03-25T12:38:31Z")

</div>

I would probably fetch up the four structure fields into a multi dimensional array in the controller and loop through that instead, something like [described here](https://www.elated.com/php-multidimensional-arrays/).

---

<div class="post-metadata">

### Author: ![j11k00](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@j11k00](https://forum.getkirby.com/u/j11k00)
#### Post date: [March 25, 2019, 1:03pm UTC](https://forum.getkirby.com/t/failing-to-pass-a-variable-to-a-snipet/13531/8 "2019-03-25T13:03:58Z")

</div>

Tanks, I’ll take a look. And thank you both for taking the time to help.

---

<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 25, 2019, 1:17pm UTC](https://forum.getkirby.com/t/failing-to-pass-a-variable-to-a-snipet/13531/9 "2019-03-25T13:17:58Z")

</div>

If the categories are defined in the controller and you pass the `$categories` to the snippet, everything should work fine, provided the snippet is called in the controller for the page where you call the snippet.

---

<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 25, 2019, 1:18pm UTC](https://forum.getkirby.com/t/failing-to-pass-a-variable-to-a-snipet/13531/10 "2019-03-25T13:18:44Z")

</div>

Maybe you can post everything together, controller, template and snippet with their filenames and content.

And what Kirby version are you using?

---

<div class="post-metadata">

### Author: ![j11k00](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@j11k00](https://forum.getkirby.com/u/j11k00)
#### Post date: [March 25, 2019, 2:46pm UTC](https://forum.getkirby.com/t/failing-to-pass-a-variable-to-a-snipet/13531/11 "2019-03-25T14:46:35Z")

</div>

Hi texnixe! Thanks for looking into this 😄 I’m using Kirby 3 and [this repo](https://github.com/starckio/starterkit-3.0.0-beta-6) as a starting point, a version of the starter kit with pretty much all the examples shown in the docs. What I’m trying to do is convert the restaurant-menu and events pages into snippets that could be pulled in to a template. All the code is identical, apart from the parts I posted in the thread earlier.

---

<div class="post-metadata">

### Author: ![j11k00](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@j11k00](https://forum.getkirby.com/u/j11k00)
#### Post date: [March 25, 2019, 3:04pm UTC](https://forum.getkirby.com/t/failing-to-pass-a-variable-to-a-snipet/13531/12 "2019-03-25T15:04:08Z")

</div>

Ah, I think get why it’s not working. The controller and the page I’m trying to pull the snippet into have different names. Apparently they have to be the same?

---

<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 25, 2019, 3:07pm UTC](https://forum.getkirby.com/t/failing-to-pass-a-variable-to-a-snipet/13531/13 "2019-03-25T15:07:45Z")

</div>

Content file name, controller file name and template file name have to match.

E.g. `example.txt` will use `example.php` controller and `example.php` template.  
E.g. `default.txt` will use `default.php` controller and `default.php` template.  
etc.

If a template with the name of the text file doesn’t exist, Kirby will fall back to the `default.php` template.

---

<div class="post-metadata">

### Author: ![j11k00](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@j11k00](https://forum.getkirby.com/u/j11k00)
#### Post date: [March 25, 2019, 3:32pm UTC](https://forum.getkirby.com/t/failing-to-pass-a-variable-to-a-snipet/13531/14 "2019-03-25T15:32:08Z")

</div>

Should have spotted that one, plunged straight into the deep end with kirby 3, and a bit out of my depth. Thank you again 😄 Can’t give enough praise on the level of support here, it’s absolutely amazing.
