Skip to content

pbMenu ​

Resource tree for navigation.

[[!pbMenu? &parent=`0` &level=`3` &tpl=`menuItem`]]

Parameters ​

ParameterDefaultDescription
parent0Root of the tree. 0 means the site root.
level3How many levels deep to walk.

Plus the shared output parameters from Intro.

Each row carries its children ​

Unlike a flat list, every item has a children collection, so one chunk renders the whole tree by calling itself:

php
<li{if $item.active} class="active"{/if}>
    <a href="{$item.url}">{$item.title}</a>
    {if $item.children}
        <ul>
            {foreach $item.children as $child}
                {'menuItem'|chunk: ['item' => $child]}
            {/foreach}
        </ul>
    {/if}
</li>

{insert} and {include} share scope

The loop variable is visible inside the included chunk — that is what makes the recursion above work. It also means a variable you set in the parent can leak in and surprise you. Name loop variables deliberately.

Examples ​

Top-level menu, one level ​

[[!pbMenu? &level=`1` &tpl=`menuItem`]]
[[!pbMenu? &parent=`[[*id]]` &level=`2` &tpl=`menuItem`]]

From Fenom ​

php
{foreach menu(0, 2) as $item}
    <a href="{$item->uri}">{$item->pagetitle}</a>
{/foreach}

© PageBlocks 2019-present