I am currently creating a menu with the Symfony package: KnpMenuBundle. I am using Bootstrap 4 as a style sheet.
Bootstrap 4 requires each list item in the navigation bar to have a nav-item class:
<li class="nav-item active"> <-- this
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
I cannot figure out how to add the "nav-item" class to a list item using KnpMenuBundle. I currently see this when I load a page:
navigation result
This is my Builder class in src / AppBundle / Menu:
namespace AppBundle\Menu;
use Knp\Menu\MenuFactory;
class Builder
{
public function mainMenu(MenuFactory $factory, array $optioins)
{
$menu = $factory->createItem('root');
$menu->setChildrenAttribute('class', 'navbar-nav mr-auto');
$menu->addChild('Home', ['route' => 'homepage']);
$menu->setChildrenAttributes(['class' => 'nav-item']);
return $menu;
}
}
My code in the base.html.twig file for generating the menu:
{{ knp_menu_render('AppBundle:Builder:mainMenu', {'currentClass': 'active'}) }}
What should I do to make it work?
source
share