templates/menu/main_menu.html.twig line 1

Open in your IDE?
  1. {% extends 'knp_menu.html.twig' %}
  2. {% macro attributes(attributes) %}
  3.     {% for name, value in attributes %}
  4.         {%- if value is not none and value is not same as(false) -%}
  5.             {{- ' %s="%s"'|format(name, value is same as(true) ? name|e : value|e)|raw -}}
  6.         {%- endif -%}
  7.     {%- endfor -%}
  8. {% endmacro %}
  9. {% block list %}
  10.     {% if item.hasChildren and options.depth is not same as(0) and item.displayChildren %}
  11.         {% import _self as knp_menu %}
  12.         {% set listAttributes = listAttributes|merge({ 'class': 'nav flex-column' }) %}
  13.         <ul{{ knp_menu.attributes(listAttributes) }}>
  14.             {{ block('children') }}
  15.         </ul>
  16.     {% endif %}
  17. {% endblock %}
  18. {% block item %}
  19.     {% if item.displayed %}
  20.         {# building the class of the item #}
  21.         {%- set classes = item.attribute('class') is not empty ? [item.attribute('class')] : [] %}
  22.         {%- set classes = classes|merge(['nav-item']) %}
  23.         {%- if matcher.isCurrent(item) %}
  24.             {%- set classes = classes|merge([options.currentClass]) %}
  25.         {%- elseif matcher.isAncestor(item, options.matchingDepth) %}
  26.             {%- set classes = classes|merge([options.ancestorClass]) %}
  27.         {%- endif %}
  28.         {%- if item.actsLikeFirst %}
  29.             {%- set classes = classes|merge([options.firstClass]) %}
  30.         {%- endif %}
  31.         {%- if item.actsLikeLast %}
  32.             {%- set classes = classes|merge([options.lastClass]) %}
  33.         {%- endif %}
  34.         {# Mark item as "leaf" (no children) or as "branch" (has children that are displayed) #}
  35.         {% if item.hasChildren and options.depth is not same as(0) %}
  36.             {% if options.branch_class is not empty and item.displayChildren %}
  37.                 {%- set classes = classes|merge([options.branch_class]) %}
  38.             {% endif %}
  39.         {% elseif options.leaf_class is not empty %}
  40.             {%- set classes = classes|merge([options.leaf_class]) %}
  41.         {%- endif %}
  42.         {%- set attributes = item.attributes %}
  43.         {%- if classes is not empty %}
  44.             {%- set attributes = attributes|merge({'class': classes|join(' ')}) %}
  45.         {%- endif %}
  46.         {# displaying the item #}
  47.         {% import _self as knp_menu %}
  48.         <li{{ knp_menu.attributes(attributes) }}>
  49.             {%- if item.uri is not empty and (not matcher.isCurrent(item) or options.currentAsLink) %}
  50.                 {{ block('linkElement') }}
  51.             {%- else %}
  52.                 {{ block('spanElement') }}
  53.             {%- endif %}
  54.             {# render the list of children#}
  55.             {%- set childrenClasses = item.childrenAttribute('class') is not empty ? [item.childrenAttribute('class')] : [] %}
  56.             {%- set childrenClasses = childrenClasses|merge(['menu_level_' ~ item.level]) %}
  57.             {%- set listAttributes = item.childrenAttributes|merge({'class': childrenClasses|join(' ') }) %}
  58.             {{ block('list') }}
  59.         </li>
  60.     {% endif %}
  61. {% endblock %}
  62. {% block linkElement %}
  63.     {% import _self as knp_menu %}
  64.     {% set linkAttributes = item.linkAttributes|merge({ 'class': 'nav-link' }) %}
  65.     <a href="{{ item.uri }}"{{ knp_menu.attributes(linkAttributes) }}>{{ block('label') }}</a>
  66. {% endblock %}