templates/Menu/menu.html.twig line 1

  1. {% extends 'knp_menu_base.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 compressed_root %}
  10. {% apply spaceless %}
  11. {{ block('root') }}
  12. {% endapply %}
  13. {% endblock %}
  14. {% block root %}
  15. {% set listAttributes = item.childrenAttributes %}
  16. {{ block('list') -}}
  17. {% endblock %}
  18. {% block list %}
  19. {% if item.hasChildren and options.depth is not same as(0) and item.displayChildren %}
  20.     {% import _self as knp_menu %}
  21.     {%- set class = listAttributes.class ?? '' %}
  22.     {%- set class = class ~ ' nav' %}
  23.     {%- set listAttributes = listAttributes|merge({'id': 'main_menu', 'class': class}) -%}
  24.     <ul{{ knp_menu.attributes(listAttributes) }}>
  25.         {{ block('children') }}
  26.     </ul>
  27. {% endif %}
  28. {% endblock %}
  29. {% block children %}
  30. {# save current variables #}
  31. {% set currentOptions = options %}
  32. {% set currentItem = item %}
  33. {# update the depth for children #}
  34. {% if options.depth is not none %}
  35. {% set options = options|merge({'depth': currentOptions.depth - 1}) %}
  36. {% endif %}
  37. {# update the matchingDepth for children #}
  38. {% if options.matchingDepth is not none and options.matchingDepth > 0 %}
  39. {% set options = options|merge({'matchingDepth': currentOptions.matchingDepth - 1}) %}
  40. {% endif %}
  41. {% for item in currentItem.children %}
  42.     {{ block('item') }}
  43. {% endfor %}
  44. {# restore current variables #}
  45. {% set item = currentItem %}
  46. {% set options = currentOptions %}
  47. {% endblock %}
  48. {% block item %}
  49. {% if item.displayed %}
  50. {# building the class of the item #}
  51.     {%- set classes = item.attribute('class') is not empty ? [item.attribute('class')] : [] %}
  52.     {%- if matcher.isCurrent(item) %}
  53.         {%- set classes = classes|merge([options.currentClass]) %}
  54.     {%- elseif matcher.isAncestor(item, options.matchingDepth) %}
  55.         {%- set classes = classes|merge([options.ancestorClass]) %}
  56.     {%- endif %}
  57.     {%- if item.actsLikeFirst %}
  58.         {%- set classes = classes|merge([options.firstClass]) %}
  59.     {%- endif %}
  60.     {%- if item.actsLikeLast %}
  61.         {%- set classes = classes|merge([options.lastClass]) %}
  62.     {%- endif %}
  63.     {# Mark item as "leaf" (no children) or as "branch" (has children that are displayed) #}
  64.     {% if item.hasChildren and options.depth is not same as(0) %}
  65.         {% if options.branch_class is not empty and item.displayChildren %}
  66.             {%- set classes = classes|merge([options.branch_class]) %}
  67.         {% endif %}
  68.     {% elseif options.leaf_class is not empty %}
  69.         {%- set classes = classes|merge([options.leaf_class]) %}
  70.     {%- endif %}
  71.     {%- set attributes = item.attributes %}
  72.     {%- if classes is not empty %}
  73.         {%- set attributes = attributes|merge({'class': classes|join(' ')}) %}
  74.     {%- endif %}
  75. {# displaying the item #}
  76.     {% import _self as knp_menu %}
  77.     <li{{ knp_menu.attributes(attributes) }}>
  78.         {%- if item.uri is not empty and (not matcher.isCurrent(item) or options.currentAsLink) %}
  79.         {{ block('linkElement') }}
  80.         {%- else %}
  81.         {{ block('spanElement') }}
  82.         {%- endif %}
  83. {# render the list of children#}
  84.         {%- set childrenClasses = item.childrenAttribute('class') is not empty ? [item.childrenAttribute('class')] : [] %}
  85.         {%- set childrenClasses = childrenClasses|merge(['menu_level_' ~ item.level]) %}
  86.         {%- set listAttributes = item.childrenAttributes|merge({'class': childrenClasses|join(' ') }) %}
  87.         {{ block('list') }}
  88.     </li>
  89. {% endif %}
  90. {% endblock %}
  91. {% block linkElement %}{% import _self as knp_menu %}
  92.     {%- set attributes = item.linkAttributes -%}
  93.     
  94.     {%- set class = attributes.class ?? '' %}
  95.     {%- set class = class ~ (class != '' ? ' ' : '') ~ 'nav-link' %}
  96.     {%- if matcher.isCurrent(item) %}
  97.         {%- set class = class ~ ' link-secondary' -%}
  98.     {%- endif -%}
  99.     
  100.     {%- set attributes = attributes|merge({'class': class}) -%}    
  101.     
  102.     <a href="{{ item.uri }}"{{ knp_menu.attributes(attributes) }}>{{ block('label') }}</a>
  103. {% endblock %}
  104. {% block spanElement %}{% import _self as knp_menu %}<span{{ knp_menu.attributes(item.labelAttributes) }}>{{ block('label') }}</span>{% endblock %}
  105. {% block label %}{% if options.allow_safe_labels and item.getExtra('safe_label', false) %}{{ item.label|raw }}{% else %}{{ item.label }}{% endif %}{% endblock %}