file menu--main.html.twig

Default theme implementation to display a menu.

Available variables:

  • menu_name: The machine name of the menu.
  • items: A nested list of menu items. Each menu item contains:
    • attributes: HTML attributes for the menu item.
    • below: The menu item child items.
    • title: The menu link title.
    • url: The menu link url, instance of \Drupal\Core\Url
    • localized_options: Menu link localized options.
Source
  1. {#
  2. /**
  3. * @file
  4. * Default theme implementation to display a menu.
  5. *
  6. * Available variables:
  7. * - menu_name: The machine name of the menu.
  8. * - items: A nested list of menu items. Each menu item contains:
  9. * - attributes: HTML attributes for the menu item.
  10. * - below: The menu item child items.
  11. * - title: The menu link title.
  12. * - url: The menu link url, instance of \Drupal\Core\Url
  13. * - localized_options: Menu link localized options.
  14. *
  15. * @ingroup templates
  16. */
  17. #}
  18. {% import _self as menus %}
  19. {#
  20. We call a macro which calls itself to render the full tree.
  21. @see http://twig.sensiolabs.org/doc/tags/macro.html
  22. #}
  23. {{ menus.menu_links(items, attributes, 0) }}
  24. {% macro menu_links(items, attributes, menu_level) %}
  25. {% import _self as menus %}
  26. {% if items %}
  27. {% if menu_level == 0 %}
  28. <ul{{ attributes.addClass('menu', 'nav', 'navbar-nav') }}>
  29. {% else %}
  30. <ul{{ attributes.addClass('dropdown-menu') }}>
  31. {% endif %}
  32. {% for item in items %}
  33. {%
  34. set item_classes = [
  35. item.is_expanded? 'expanded',
  36. item.is_expanded and menu_level == 0 ? 'dropdown',
  37. item.in_active_trail ? 'active',
  38. ]
  39. %}
  40. {% if menu_level == 0 and item.is_expanded %}
  41. <li{{ item.attributes.addClass(item_classes) }}>
  42. <a href="{{ item.url }}" class="dropdown-toggle" data-toggle="dropdown">{{ item.title }} <span class="caret"></span></a>
  43. {% else %}
  44. <li{{ item.attributes.addClass(item_classes) }}>
  45. {{ link(item.title, item.url) }}
  46. {% endif %}
  47. {% if item.below %}
  48. {{ menus.menu_links(item.below, attributes.removeClass('nav', 'navbar-nav'), menu_level + 1) }}
  49. {% endif %}
  50. </li>
  51. {% endfor %}
  52. </ul>
  53. {% endif %}
  54. {% endmacro %}

Related topics