Independent Sub Menu not rendered at all

I tried this code from the cookbook but it doesn’t even render at all. No nav no ul.

<?php

$items = false;

// get the open item on the first level
if($root = $pages->findOpen()) {

  // get visible children for the root item
  $items = $root->children()->listed();
}

// only show the menu if items are available
if($items and $items->isNotEmpty()):

?>
<nav>
  <ul>
    <?php foreach($items as $item): ?>
    <li><a<?php e($item->isOpen(), ' class="active"') ?> href="<?= $item->url() ?>"><?= $item->title()->html() ?></a></li>
    <?php endforeach ?>
  </ul>
</nav>
<?php endif ?>

If I take away the if clause “isNotEmpty”, I get a nav element and the ul, but no li.

How can I have no pages for the script to find, when I actually do have them?

If I use my own menu snippet - which is pretty much the one from the docs - it works. I just need a sub menu thing for one page/ item.

I guess, I misunderstood this menu example in that, I am not sure what kind of menu this is.

Then either the currently open page ($root) has no listed children or no children at all.

Yes, there is just one page with a subpage/ child. But I need to show the submenu if a user clicks on the menu item that has a subpage. But of course the whole menu should be visible on every page, not only on a page that has children. So - I somehow misunderstood something.

Do a dump($root) to see what you get.

Kirby\Cms\Structure Object
(
)

$root should be a page object, not a structure object. Maybe you can post the complete code?

The site.yml:



tabs:
  menu:
    type: structure
    fields:
      mainMenu:
        type: pages
        label: Mainmenu item
        #max: 1
      hasSubmenu:
        type: toggle
        text: Include a submenu?
      subMenu:
        type: pages
        label: Submenu items
        when:
          hasSubmenu: true

  content:
    label: Seiten
    icon: document
    layout: cards
    width: 1/4

    sections:
      info:
        label: Info
        type: info
        theme: positive
        text: |
          Dies ist die Übersicht über alle Seiten der Website. Einfach auf eine der Seiten klicken und Inhalte eintragen oder auf einen der Tabs oben.

          Bitte auch die Bedienungsanleitung beachten, die über das Menü-Symbol (&#9776;) oben links erreicht werden kann!

      pages:
        type: pages
        headline: Seiten
        layout: cards
        create: default


  kontakt:
    label: kontakt
    icon: email
    fields:
      address:
        label: Büro
        type: writer
        inline: true
      firmensitz:
        label: Firmensitz
        type: writer
        inline: true
      email:
        label: Email
        type: email
      phone:
        label: Telefon 1
        type: tel
      phoneAlt:
        label: Telefon 2
        type: tel

  impressum:
    label: Impressum
    icon: text
    fields:
      legal:
        label: Impressum
        type: textarea
        size: medium

  datenschutz:
    label: Datenschutz
    icon: wheelchair

    fields:
      dsgvoText:
        label: Text
        type: textarea
        size: large

options:
  drafts: false
  changeTitle: false
  changeStatus: false
  delete: false
  changeSlug: false
  changeUrl: false
  files: true

The header snippet

  <header class="site-header">
    <a class="logo-box" href="<?= $site->url() ?>">
      <?= asset('assets/icons/best-logo.png') ?>
    </a>

    <a class="menu-btn menu-trigger">
  <?= asset('assets/icons/menu-icon.svg')->read() ?>
</a>


<?php 
$menu = $site->menu()->toStructure();
if ($menu->isNotEmpty()) : ?>
<nav>
  <ul>
    <?php foreach ($menu as $item): ?>
      <?php if ($mainMenuItem = $item->mainMenu()->toPage()) : ?>
      <li><a href="<?= $mainMenuItem->url() ?>"><?= $mainMenuItem->title() ?></a>
      <?php endif ?>
      <?php $subMenu = $item->subMenu()->toPages(); ?>
      <?php if ($item->hasSubmenu()->isTrue() && $subMenu->isNotEmpty()) : ?>
        <ul class="submenu-list">
          <?php foreach ($subMenu as $subItem) : ?>
          <li><a href="<?= $subItem->url() ?>"><?= $subItem->title() ?></a></li>
          <?php endforeach ?>
        </ul>
      </li>
      <?php endif ?>
    <?php endforeach ?>
  </ul>
</nav>
<?php endif ?>
<?php dump($menu) ?>
  </header>

``

Hm, I’m a bit at a loss, what has this got to do with the code from your first post?

This also has the same result, as if my pages don’t exist.

This blueprint looks somehow wrong as well…

I took it from the docs and put into a tabs section in my ste.yml.

Not sure if this is what I need, I just wanted to try it and see what it does.

Should look like this:

tabs:
  tabname:
    label: Label for tab
    fields:
      menu:
        type: structure
       # etc.

Ah… this was it!