Passing a variable to a Snippet: Undefined variable error

Hello,

I created a snippet called /site/snippets/menu.php:

<?php
if ($menu === 'main'): ?>
 (...)
<?php else: ?>
 (...)
<?php endif ?>

In my header.php I call the snippet like this:
<?php snippet('menu', ['menu' => 'main']) ?>

I get this error in the Kirby CMS Debugger: Undefined variable: menu.

Why is the variable menu undefined in my case? Not sure if it matters but I have a multilang setup with only 1 language defined. I took this guide as a reference: https://getkirby.com/docs/guide/templates/snippets#passing-variables-to-snippets.

Best Regards,

Try like this…

<?php
if ($menu == "main"): ?>
 (...)
<?php else: ?>
 (...)
<?php endif ?>

Hey,

thanks for your reply. Sadly, replacing the ' with double quotes (") didn’t change anything.

Best Regards,

@jimbobrjames That shouldn’t make a difference, actually,

@warg Sure the error is caused by that first line the snippet? I can’t reproduce this issue.

Strange, works for me in my current project.

Hi @texnixe!

According to the debugger, the error is caused in the line of the if. At least that line is highlighted in the code snipped shown in the debugger. And $menu is only used there due to the fact that it is a static value in the template; not a vairable itself.

Best Regards,

ahhh spotted it…

Change

<?php snippet('menu', ['menu' => 'main']) ?>

Too…
<?= snippet('menu', ['menu' => 'main']) ?>

@jimbobrjames Why do you want to echo the snippet?

1 Like

Maybe you are calling the snippet someplace else?

1 Like

Always have done. Like for 4 years… must have got it from the docs or something :slight_smile: Is that wrong then?

1 Like

This didn’t help too.

I took a screenshot from the debugger:

Would be useful to see the backtrace, where the snippet is called and with what parameters.

You were right @texnixe! I found another spot I forgot about in an old template. There was a call without the value passed as a variable. Notepad++ search helped me!

Sorry for wasting your time with this obvious mistake. I didn’t remember I placed it there in the past; maybe I did it because I followed a tutorial back then.

Could you be working on the page where you dont pass the menu variable? You can check the variable:

if (isset($menu) and $menu == 'main'):
1 Like

No problem, happens to all of us.

Checking if the variable is set makes sense though to prevent such errors in the first place.

1 Like

The backtrace is a good hint! I found out that at some steps earlier, there is the old template spot with the mistake marked. Now I know how/that I should check the backtrace in other cases like this!

The variable should be set all time to at least one of two possible values so it is fine if it errors.

Yes, but there’s a difference between what should be and what actually happens. So it is good practice to either check for the variable and/or to set a default value.

1 Like

. . . true, a default value makes sense here! I will do this. Thanks to all of you!