Menus
Main menu
Sub menus
The independent sub menu
The independent sub sub menu
Tree menu
The recursive treemenu
snippet renders a menu of all your site's pages and subpages.
Usage
Save the code as treemenu.php
in the /site/snippets
folder. Saving it as a snippet and with this exact name is key in this case. If you just include the code in your template or if you rename the snippet, it won't work, because the snippet is called from within the snippet.
Include it in your code using the snippet()
helper:
If you don't want it to start from the first level, pass it a set of pages, where it should start. i.e.:
Nested menu
Custom menu
Selectbox menu
Breadcrumb menu
Styling and markup
All the examples above are pretty raw. You probably want to add additional classes or more markup to the list elements to style them. That's all up to you. Here are two more little helpers, which will make your life easier:
Dedicated CSS selectors for individual pages
It's often necessary to style particular items in a menu without touching the others – for example if you want to add icons. This can be done by adding dedicated selectors:
In your template
In your CSS
The uid()
method displays the folder name of each item without the prepended number.
Note that the UID is subject to change if the user is allowed to change it. It might therefore make sense to either disallow changing of the UID or use an identifier that may not be changed.
Page Depth
Especially in nested menus it can be helpful to have an additional selector that indicates the depth of the item, for example for indenting list items.
In your template
In your CSS
The entire API
Of course this is only a minimal extract of all possibilities. The entire Kirby API is there to get the most out of your ideas, so check out the Kirby Reference for additional methods you can use.