Passing variables to a snippet - Again, I know

Hi everyone. I have read everywhere, even on similar topics here in the forum, and I can’t find where I’m messing things up. So any help will be welcome.

I have this Mixed Menu II (from the Cookbook) snippet, which I call from my header.php snippet.

This is the call line from the header.php

<?php
$items = $site->nestedMenu()->toStructure();
$loc = "header";
?>
....
<?php snippet('menuitem-list', ['items' => $items, 'loc' => $loc]) ?>

And this is the menuitem-list.php snippet:

<menu>
  <?php foreach ($items as $item): ?>
  <?php if ($item->excludeFromHeader()->isFalse()):?>
  <li<?= e($item->hasSubmenu()->isTrue() && $loc === "header", ' class="dropdown dropdown__arrow"') ?>>
    <a href="<?= $item->link()->toUrl() ?>"><?= $item->linkTitle()->or($item->link()->html()) ?></a>
    <?php $subMenuItems = $item->subMenu()->toStructure(); ?>
    <?php if ($item->hasSubmenu()->isTrue() && $subMenuItems->isNotempty()): ?>
    <?php snippet('menuitem-list', ['items' => $subMenuItems]) ?>
    <?php endif ?>
  </li>
  <?php endif ?>
  <?php endforeach ?>
</menu>

Note I have used the $loc variable to set the CSS clases to the <li> element (line 4).

This works.

However, when I try to use anywhere else the $loc variable, Kirby throws an error and says $loc is Unknown. For example, if I add at the top, before the menu tag, this:

<?php echo $loc ?>

Then I get this

[Fri Jun  7 10:50:50 2024] Whoops\Exception\ErrorException: Undefined variable $loc in /home/yaidel/Kirby/plainkit-main/site/snippets/menuitem-list.php:2
Stack trace:
#0 /home/yaidel/Kirby/plainkit-main/site/snippets/menuitem-list.php(2): Whoops\Run->handleError()
#1 /home/yaidel/Kirby/plainkit-main/kirby/src/Filesystem/F.php(425): include('...')
#2 /home/yaidel/Kirby/plainkit-main/kirby/src/Filesystem/F.php(364): Kirby\Filesystem\F::loadIsolated()
#3 /home/yaidel/Kirby/plainkit-main/kirby/src/Filesystem/F.php(372): Kirby\Filesystem\F::Kirby\Filesystem\{closure}()
#4 /home/yaidel/Kirby/plainkit-main/kirby/src/Toolkit/Tpl.php(36): Kirby\Filesystem\F::load()
#5 /home/yaidel/Kirby/plainkit-main/kirby/src/Template/Snippet.php(172): Kirby\Toolkit\Tpl::load()
#6 /home/yaidel/Kirby/plainkit-main/kirby/config/components.php(301): Kirby\Template\Snippet::factory()
#7 /home/yaidel/Kirby/plainkit-main/kirby/src/Cms/App.php(1546): Kirby\Cms\Core->{closure}()
#8 /home/yaidel/Kirby/plainkit-main/kirby/config/helpers.php(531): Kirby\Cms\App->snippet()
#9 /home/yaidel/Kirby/plainkit-main/site/snippets/menuitem-list.php(10): snippet()
#10 /home/yaidel/Kirby/plainkit-main/kirby/src/Filesystem/F.php(425): include('...')
#11 /home/yaidel/Kirby/plainkit-main/kirby/src/Filesystem/F.php(364): Kirby\Filesystem\F::loadIsolated()
#12 /home/yaidel/Kirby/plainkit-main/kirby/src/Filesystem/F.php(372): Kirby\Filesystem\F::Kirby\Filesystem\{closure}()
#13 /home/yaidel/Kirby/plainkit-main/kirby/src/Toolkit/Tpl.php(36): Kirby\Filesystem\F::load()
#14 /home/yaidel/Kirby/plainkit-main/kirby/src/Template/Snippet.php(172): Kirby\Toolkit\Tpl::load()
#15 /home/yaidel/Kirby/plainkit-main/kirby/config/components.php(301): Kirby\Template\Snippet::factory()
#16 /home/yaidel/Kirby/plainkit-main/kirby/src/Cms/App.php(1546): Kirby\Cms\Core->{closure}()
#17 /home/yaidel/Kirby/plainkit-main/kirby/config/helpers.php(531): Kirby\Cms\App->snippet()
#18 /home/yaidel/Kirby/plainkit-main/site/snippets/header.php(18): snippet()
#19 /home/yaidel/Kirby/plainkit-main/kirby/src/Filesystem/F.php(425): include('...')
#20 /home/yaidel/Kirby/plainkit-main/kirby/src/Filesystem/F.php(364): Kirby\Filesystem\F::loadIsolated()
#21 /home/yaidel/Kirby/plainkit-main/kirby/src/Filesystem/F.php(372): Kirby\Filesystem\F::Kirby\Filesystem\{closure}()
#22 /home/yaidel/Kirby/plainkit-main/kirby/src/Toolkit/Tpl.php(36): Kirby\Filesystem\F::load()
#23 /home/yaidel/Kirby/plainkit-main/kirby/src/Template/Snippet.php(172): Kirby\Toolkit\Tpl::load()
#24 /home/yaidel/Kirby/plainkit-main/kirby/config/components.php(301): Kirby\Template\Snippet::factory()
#25 /home/yaidel/Kirby/plainkit-main/kirby/src/Cms/App.php(1546): Kirby\Cms\Core->{closure}()
#26 /home/yaidel/Kirby/plainkit-main/kirby/config/helpers.php(531): Kirby\Cms\App->snippet()
#27 /home/yaidel/Kirby/plainkit-main/site/templates/default.php(8): snippet()
#28 /home/yaidel/Kirby/plainkit-main/kirby/src/Filesystem/F.php(425): include('...')
#29 /home/yaidel/Kirby/plainkit-main/kirby/src/Filesystem/F.php(364): Kirby\Filesystem\F::loadIsolated()
#30 /home/yaidel/Kirby/plainkit-main/kirby/src/Filesystem/F.php(372): Kirby\Filesystem\F::Kirby\Filesystem\{closure}()
#31 /home/yaidel/Kirby/plainkit-main/kirby/src/Toolkit/Tpl.php(36): Kirby\Filesystem\F::load()
#32 /home/yaidel/Kirby/plainkit-main/kirby/src/Template/Template.php(163): Kirby\Toolkit\Tpl::load()
#33 /home/yaidel/Kirby/plainkit-main/kirby/src/Cms/Page.php(1017): Kirby\Template\Template->render()
#34 /home/yaidel/Kirby/plainkit-main/kirby/src/Cms/App.php(775): Kirby\Cms\Page->render()
#35 /home/yaidel/Kirby/plainkit-main/kirby/src/Cms/App.php(1189): Kirby\Cms\App->io()
#36 /home/yaidel/Kirby/plainkit-main/index.php(5): Kirby\Cms\App->render()
#37 /home/yaidel/Kirby/plainkit-main/kirby/router.php(14): require('...')
#38 {main}

But more strange even, the following does works:

<?php echo isset($loc)? $loc: '';  ?>

Any clue what is going on here? Thanks in advance.

Because you don’t pass it along here!

Ups! Yes, that was it. Thank you so much for your help, Sonja.