docs icon indicating copy to clipboard operation
docs copied to clipboard

Bolt theme templating, set menu active from record template

Open GDmac opened this issue 10 years ago • 0 comments

A work-around to set a menu item to active, pass an activepage variable to the macro.

When on a record-template for a contenttype, before including header.twig, you set activepage {% set activepage = 'menulabel' %} This will seep thru to the menu macro, and checks for {% if item|current or activemenu|lower == item.label|lower %} active{% endif %}

{# -------------------------------------------------- #}
{# record_template: events_detail.twig #}

  {% set activepage = 'bijeenkomsten' %}
  {% include '_header.twig' with {'activepage':activepage} %}
  ...
{# -------------------------------------------------- #}
{# _header.twig #}
  ...
  {{ menu('main', '_sub_menu.twig', {'activemenu':(activepage is defined ? activepage : '')}) }}
  ...
{# -------------------------------------------------- #}
{# _sub_menu.twig #}

{% macro display_menu_item(item, loop, activemenu) %}
    {% from _self import display_menu_item %}
    <li class="index-{{ loop.index }}{% if loop.first %} first{% endif %}{% if loop.last %} last{% endif %}{% if item.submenu is defined %} has-dropdown{% endif %}{% if item|current or activemenu|lower == item.label|lower %} active{% endif %}">
        <a href="{{ item.link }}" {% if item.title is defined %}title='{{ item.title|escape }}'{% endif %}
           class='{% if item.class is defined %}{{item.class}}{% endif %}'>
            {{item.label}}
        </a>
        {% if item.submenu is defined %}
            <ul class="dropdown">
            {% for submenu in item.submenu %}
                {{ display_menu_item(submenu, loop, activemenu) }}
            {% endfor %}
            </ul>
        {% endif %}
    </li>
{% endmacro %}

{% from _self import display_menu_item %}


{% for item in menu %}
    {% if item.label is defined %}
        {{ display_menu_item(item, loop, activemenu) }}
    {% endif %}
{% endfor %}

GDmac avatar Mar 01 '15 12:03 GDmac