Navigation
Basics
Pimcore comes with a standard navigation implementation in the form of a Twig extension. It builds a navigation container based on the existing document structure. The process of rendering is divided into 2 steps:
- Build the navigation:
{% set nav = pimcore_build_nav({active: activeDocument, root: navigationRootDocument}) %}
- Render the navigation:
pimcore_render_nav(nav)
orpimcore_nav_renderer('menu').render(nav)
The building step does not necessarily need to happen in the view script. In fact the view helper just forwards the
build()
call to thePimcore\Navigation\Builder
service. You can also build the navigation in your controller or a service and pass the navigation object to the view.
Only documents are included in this structure, Folders are ignored, regardless of their navigation properties.
{# get root node if there is no document defined (for pages which are routed directly through static route) #}
{% if not document is defined or not document %}
{% set document = pimcore_document(1) %}
{% endif %}
{# get the document which should be used to start in navigation | default home #}
{% set navStartNode = document.getProperty('navigationRoot') %}
{% if not navStartNode is instanceof('\\Pimcore\\Model\\Document\\Page') %}
{% set navStartNode = pimcore_document(1) %}
{% endif %}
{% set mainNavigation = pimcore_build_nav({
active: document,
root: navStartNode
}) %}
{# later you can render the navigation #}
{{ pimcore_render_nav(mainNavigation) }}
Having set up the navigation container as shown above, you can easily use it to render a navigation tree, menus, or breadcrumbs.
Meta Navigation - Only the 1st Level
{{ pimcore_nav_renderer('menu').renderMenu(mainNavigation,{
maxDepth: 1,
ulClass: 'nav navbar-nav'
}) | raw }}
{#alternatively, you can use the render function to use the given renderer and render method#}
<div class="my-menu">
{# the menu() shortcut is not available in twig #}
{{ pimcore_render_nav(mainNavigation, 'menu', 'renderMenu', {
maxDepth: 1,
ulClass: 'nav navbar-nav'
}) }}
</div>
Meta Navigation - Multilevel
<div class="my-menu">
{# you can use array for ulClass to provide depth level classes #}
{{ pimcore_render_nav(mainNavigation, 'menu', 'renderMenu', {
maxDepth: 2,
ulClass: {
0: 'nav navbar-nav',
1: 'nav navbar-nav-second',
2: 'nav navbar-nav-third'
}
}) }}
{# alternatively, you can use 'default' key to apply class on all depth levels #}
{{ pimcore_render_nav(mainNavigation, 'menu', 'renderMenu', {
maxDepth: 2,
ulClass: {
default: 'nav navbar-nav'
}
}) }}
</div>
Breadcrumbs
<div class="my-breadcrumbs">
<a href="/">{{ 'Home'|trans }}</a> >
{{ pimcore_render_nav(mainNavigation, 'breadcrumbs') }}
</div>