templates/menu/knp_menu.html.twig line 1

Open in your IDE?
  1. {% extends 'knp_menu.html.twig' %}
  2. {% macro setCssClassAttribute(item, type, add) %}
  3.     {% set getter = 'get' ~ type %}
  4.     {% set setter = 'set' ~ type %}
  5.     {% set value = attribute(item, getter, ['class']) %}
  6.     {% if value is iterable %}
  7.         {% set value = value|join(' ') %}
  8.     {% endif %}
  9.     {% do attribute(item, setter, ['class', value ~ ' ' ~ add]) %}
  10. {% endmacro %}
  11. {% block item %}
  12.     {% import "knp_menu.html.twig" as macros %}
  13.     {#
  14.     As multiple level is not currently supported by bootstrap 4
  15.     This requires you to install
  16.     https://github.com/bootstrapthemesco/bootstrap-4-multi-dropdown-navbar
  17.     And set the the use_multilevel = true
  18.     #}
  19.     {% set use_multilevel = false %}
  20.     {% if item.displayed %}
  21.         {%- set attributes = item.attributes %}
  22.         {%- set is_dropdown = attributes.dropdown|default(false) %}
  23.         {%- set divider_prepend = attributes.divider_prepend|default(false) %}
  24.         {%- set divider_append = attributes.divider_append|default(false) %}
  25.         {# unset bootstrap specific attributes #}
  26.         {%- set attributes = attributes|merge({'dropdown': null, 'divider_prepend': null, 'divider_append': null }) %}
  27.         {%- if divider_prepend %}
  28.             {{ block('dividerElement') }}
  29.         {%- endif %}
  30.         {# building the class of the item #}
  31.         {%- set classes = item.attribute('class') is not empty ? [item.attribute('class'), 'nav-item'] : ['nav-item'] %}
  32.         {%- if matcher.isCurrent(item) %}
  33.             {%- set classes = classes|merge([options.currentClass]) %}
  34.         {%- elseif matcher.isAncestor(item, options.depth) %}
  35.             {%- set classes = classes|merge([options.ancestorClass]) %}
  36.         {%- endif %}
  37.         {%- if item.actsLikeFirst %}
  38.             {%- set classes = classes|merge([options.firstClass]) %}
  39.         {%- endif %}
  40.         {%- if item.actsLikeLast %}
  41.             {%- set classes = classes|merge([options.lastClass]) %}
  42.         {%- endif %}
  43.         {# building the class of the children #}
  44.         {%- set childrenClasses = item.childrenAttribute('class') is not empty ? [item.childrenAttribute('class')] : [] %}
  45.         {%- set childrenClasses = childrenClasses|merge(['menu_level_' ~ item.level]) %}
  46.         {# adding classes for dropdown #}
  47.         {%- if is_dropdown %}
  48.             {%- set classes = classes|merge(['dropdown']) %}
  49.             {%- set childrenClasses = childrenClasses|merge(['dropdown-menu']) %}
  50.         {%- endif %}
  51.         {# putting classes together #}
  52.         {%- if classes is not empty %}
  53.             {%- set attributes = attributes|merge({'class': classes|join(' ')}) %}
  54.         {%- endif %}
  55.         {%- set listAttributes = item.childrenAttributes|merge({'class': childrenClasses|join(' ') }) %}
  56.         <li{{ macros.attributes(attributes) }}>
  57.             {# displaying the item #}
  58.             {%- if is_dropdown %}
  59.                 {{ block('dropdownElement') }}
  60.             {%- elseif item.uri is not empty and (not item.current or options.currentAsLink) %}
  61.                 {{ block('linkElement') }}
  62.             {%- else %}
  63.                 {{ block('spanElement') }}
  64.             {%- endif %}
  65.             {%- if divider_append %}
  66.                 {{ block('dividerElement') }}
  67.             {%- endif %}
  68.             {% if item.hasChildren and options.depth is not same as(0) and item.displayChildren %}
  69.                 {{ block('dropdownlinks') }}
  70.             {% endif %}
  71.         </li>
  72.     {% endif %}
  73. {% endblock %}
  74. {% block dropdownlinks %}
  75.     {% if use_multilevel %}
  76.         <ul class="dropdown-menu">
  77.     {% else %}
  78.         <div class="dropdown-menu">
  79.     {% endif %}
  80.     {% for item in item.children %}
  81.         {{ block('renderDropdownlink') }}
  82.         {% if use_multilevel and item.hasChildren and options.depth is not same as(0) and item.displayChildren %}
  83.             {{ block('dropdownlinks') }}
  84.         {% endif %}
  85.     {% endfor %}
  86.     {% if not use_multilevel %}
  87.         </div>
  88.     {% else %}
  89.         </ul>
  90.     {% endif %}
  91. {% endblock %}
  92. {% block renderDropdownlink %}
  93.     {% import _self as ownmacro %}
  94.     {%- set divider_prepend = item.attributes.divider_prepend|default(false) %}
  95.     {%- set divider_append = item.attributes.divider_append|default(false) %}
  96.     {%- set attributes = item.attributes|merge({'dropdown': null, 'divider_prepend': null, 'divider_append': null }) %}
  97.     {% if use_multilevel %}
  98.         <li>
  99.     {% endif %}
  100.     {%- if divider_prepend %}
  101.         {{ block('dividerElementDropdown') }}
  102.     {%- endif %}
  103.     {%- if item.uri is not empty and (not item.current or options.currentAsLink) %}
  104.         {{ ownmacro.setCssClassAttribute(item, 'LinkAttribute', 'dropdown-item') }}
  105.         {{ block('linkElement') }}
  106.     {%- else %}
  107.         {{ block('spanElementDropdown') }}
  108.     {%- endif %}
  109.     {%- if divider_append %}
  110.         {{ block('dividerElementDropdown') }}
  111.     {%- endif %}
  112.     {% if use_multilevel %}
  113.         </li>
  114.     {% endif %}
  115. {% endblock %}
  116. {% block spanElementDropdown %}
  117.     {% import "knp_menu.html.twig" as macros %}
  118.     {% import _self as ownmacro %}
  119.     {{ ownmacro.setCssClassAttribute(item, 'LabelAttribute', 'dropdown-header') }}
  120.     <div {{ macros.attributes(item.labelAttributes) }}>
  121.         {% if item.attribute('icon') is not empty  %}
  122.             <i class="{{ item.attribute('icon') }}"></i>
  123.         {% endif %}
  124.         {{ block('label') }}
  125.     </div>
  126. {% endblock %}
  127. {% block dividerElementDropdown %}
  128.     <div class="dropdown-divider"></div>
  129. {% endblock %}
  130. {% block dividerElement %}
  131.     {% if item.level == 1 %}
  132.         <li class="divider-vertical"></li>
  133.     {% else %}
  134.         <li class="divider"></li>
  135.     {% endif %}
  136. {% endblock %}
  137. {% block linkElement %}
  138.     {% import "knp_menu.html.twig" as macros %}
  139.     {% import _self as ownmacro %}
  140.     {{ ownmacro.setCssClassAttribute(item, 'LinkAttribute', 'nav-link') }}
  141.     <a href="{{ item.uri }}"{{ macros.attributes(item.linkAttributes) }}>
  142.         {% if item.attribute('icon') is not empty  %}
  143.             <i class="{{ item.attribute('icon') }}"></i>
  144.         {% endif %}
  145.         {{ block('label') }}
  146.     </a>
  147. {% endblock %}
  148. {% block spanElement %}
  149.     {% import "knp_menu.html.twig" as macros %}
  150.     {% import _self as ownmacro %}
  151.     {{ ownmacro.setCssClassAttribute(item, 'LabelAttribute', 'navbar-text') }}
  152.     <span {{ macros.attributes(item.labelAttributes) }}>
  153.         {% if item.attribute('icon') is not empty  %}
  154.             <i class="{{ item.attribute('icon') }}"></i>
  155.         {% endif %}
  156.         {{ block('label') }}
  157.     </span>
  158. {% endblock %}
  159. {% block dropdownElement %}
  160.     {% import "knp_menu.html.twig" as macros %}
  161.     {%- set classes = item.linkAttribute('class') is not empty ? [item.linkAttribute('class')] : [] %}
  162.     {%- set classes = classes|merge(['dropdown-toggle', 'nav-link']) %}
  163.     {%- set attributes = item.linkAttributes %}
  164.     {%- set attributes = attributes|merge({'class': classes|join(' ')}) %}
  165.     {%- set attributes = attributes|merge({'data-toggle': 'dropdown'}) %}
  166.     <a href="#"{{ macros.attributes(attributes) }}>
  167.         {% if item.attribute('icon') is not empty  %}
  168.             <i class="{{ item.attribute('icon') }}"></i>
  169.         {% endif %}
  170.         {{ block('label') }}
  171.         <b class="caret"></b>
  172.     </a>
  173. {% endblock %}
  174. {% block label %}{{ item.label|trans }}{% endblock %}