For a recent project, I needed to include KnpMenuBundle in a Symfony2 project. This project was styled using the famous Twitter Bootstrap. I’ve found many articles describing how to do this but all of them didn’t exactly resulted in something I wanted: a web application with a navigation bar including icons and dropdown menu’s. In addition, I needed a “active” class indicating which item was visited, including parent/child items.
The files below belong to a tutorial which can be found here: http://nielsmouthaan.nl/symfony2-knpmenubundle-font-awesome-twitter-bootstrap-integration-navigation-bar-including-icons-and-dropdown-menus/
I worked out a way to do this without using a voter or over-riding the ancestorClass, i.e. "active link highlighting on both the current item and its containing dropdown link". See the code snippet below where I am over-riding the linkElement block:
The trick is this bit:
Because the ancestor only gets an ancestor class if one of its children are active, you simply add an 'active' class to the classes for the ancestor link element. Job done! :)
HTH