Skip to content

Twig

phil/menu-bundle
FunctionGives
menu(name)The Menu, built, decorated and marked for the request
menu_breadcrumb(name)The items from the top to the current one
menu_breadcrumb_schema(name)The breadcrumb as a schema.org BreadcrumbList

A menu is built and decorated once per request and name: a page that shows the list, the breadcrumb and its JSON-LD runs the builder and each decorator once.

The templates

{% set menu = menu('main') %}
{% include '@PhilMenu/menu.html.twig' %}
{% include '@PhilMenu/breadcrumb.html.twig' %}
{% include '@PhilMenu/cards.html.twig' with {items: menu.find('games').children} %}
TemplateShows
menu.html.twigA <nav> of nested lists; the current link has aria-current="page", its ancestors data-active-trail; an item's attributes are its <li>'s
breadcrumb.html.twigA <nav> of the trail, the last item aria-current="page"; nothing when no item is current
cards.html.twigThe items, or the given items, as cards: icon as a class, label, description, counts, last change

Labels are translated in the menu's domain; the templates' own words — Breadcrumb, the counts — are in the phil_menu domain, in English and French. An application overrides a template in templates/bundles/PhilMenuBundle/.

schema.org

menu_breadcrumb_schema(name) gives the breadcrumb of a menu as a schema.org BreadcrumbList: each item a ListItem at its position, with its name and its absolute URL, as search engines read them. The name is the label translated in the menu's domain, as the templates show it. The URL of an item with a route is generated by the router, absolute; an address on the site, /about, gets the request's scheme and host; an address on the web is kept. An item with an address relative to the page, #top, has no URL in the list. The translator and the router are the application's translator and router services, when it has them: without a translator, the label is the name, and without a router, an item with a route has no URL.

BreadcrumbSchema::of($items, $name, $url) builds the same list outside Twig, given how to name an item and its absolute URL.

<script type="application/ld+json">{{ menu_breadcrumb_schema('main')|json_encode(constant('JSON_UNESCAPED_SLASHES') b-or constant('JSON_UNESCAPED_UNICODE') b-or constant('JSON_HEX_TAG'))|raw }}</script>