current active classes are added to it: <li class="current active"> <a href="#link">Current link</a> </li> KNPMenuBundle , where you can customize the highlight of the active item, although if you do it for the first time, it may seem complicated. But if you have a simple HTML menu with only several levels of nesting and you are too lazy to transfer it into OOP menu like KNPMenuBundle , I suggest ActiveMenuItemBundle library, which will help you in highlighting the current menu item.ActiveMenuItemBundle for a symfony project with Composer : { "require": { "bw/active-menu-item-bundle": "1.1.*@dev" } } app/AppKernel.php : public function registerBundles() { $bundles = array( // other bundles... new BW\ActiveMenuItemBundle\BWActiveMenuItemBundle(), ); is_active filter . <li class="{{ 'route_name'|is_active }}"> <a href="{{ path('route_name') }}">Current link</a> </li> current active line.is_active function , passing the array of child routes to it with the first argument, and the route of this menu item with the second argument. <li class="{{ is_active(['child_route_1', 'child_route_2'], 'parent_route') }}"> <a href="{{ path('parent_route') }}">Parent link</a> <ul> <li class="{{ 'child_route_1'|is_active }}"> <a href="{{ path('child_route_1') }}">Child link</a> </li> <li class="{{ 'child_route_2'|is_active }}"> <a href="{{ path('child_route_2') }}">Current link</a> </li> </ul> </li> child_route_2 coincided with the Current link reference, then the current active classes will be assigned to it, and only the active class will be assigned to its parent with the parent_route parent_route .is_active_uri filter and the is_active_uri function that work exactly the same way, you can pass the request URI into them, which can be generated by the path function supplied from the symfony box. <li class="{{ path('route_name')|is_active_uri }}"> <a href="{{ path('route_name') }}">Current link</a> </li> app/config/routing_dev.yml for the dev mode: bw_active_menu_item: resource: "@BWActiveMenuItemBundle/Resources/config/routing.yml" prefix: /bw/demo/active-menu-item php app/console server:run in dev mode and go to localhost:8000/bw/demo/active-menu-item/index localhost:8000/bw/demo/active-menu-item/index . Here is the Twig template demo code .Source: https://habr.com/ru/post/224185/
All Articles