Winter CMS resources and help articles

Simple and to the point. Optimized by the community.

Build menu from StaticMenu

0
by rezgui, last modified on November 1st, 2024

Required plugin winter.page

[staticMenu mainMenu]
code = "menu_principale"
==
{% import _self as nav %}
{% macro render_menu(items) %}
    {% import _self as nav %}

            {% for item in items | filter(item => not item.viewBag.isHidden) %}
                <li class="{{ item.isActive ? 'current-menu-item' }} 
                           {{ item.isChildActive ? 'current-menu-item' }} 
                           {{ item.viewBag.cssClass }}">

            {% if item.url %}
                <a href="{{ item.url }}" {{ item.viewBag.isExternal ? 'target="_blank"' }}>
                    {{ item.code ? item.code|_ : item.title }}
                </a>
            {% else %}
                <a> {{ item.code ? item.code|_ : item.title }} </a>
            {% endif %}

            {% if item.items %}
                <ul class="sub-menu">
                    {{ nav.render_menu(item.items) }}
                </ul>
            {% endif %}
        </li>
    {% endfor %}
{% endmacro %}

and with Twig tag you can make menu :

{% if mainMenu.menuItems %}
    <ul>
        {{ nav.render_menu(mainMenu.menuItems) }}
    </ul>
{% endif %}

Discussion

0 comments

We use cookies to measure the performance of this website. Do you want to accept these cookies?