11

I was wondering, what is the best way to change the "current" class to be "active" so Bootstrap will work correctly with it?

I thought about copying and overriding the knp_menu_html.twig but i think its not the best way...

Anyway better doing it?

doron
  • 1,508
  • 3
  • 18
  • 27

2 Answers2

26

To apply default options in all your application, you can set the knp_menu.renderer.twig.options parameter like this:

// app/config/services.yml
parameters:
    knp_menu.renderer.twig.options:
        currentClass: active

Default options of the Knp\Menu\Renderer\TwigRenderer are:

    $this->defaultOptions = array_merge(array(
        'depth' => null,
        'matchingDepth' => null,
        'currentAsLink' => true,
        'currentClass' => 'current',
        'ancestorClass' => 'current_ancestor',
        'firstClass' => 'first',
        'lastClass' => 'last',
        'template' => $template,
        'compressed' => false,
        'allow_safe_labels' => false,
        'clear_matcher' => true,
        'leaf_class' => null,
        'branch_class' => null,
    ), $defaultOptions);
Lionel Gaillard
  • 3,023
  • 1
  • 21
  • 20
  • Is it possible to set these options inside MenuBuilder class? – Peyman Mohamadpour Oct 27 '17 at 07:07
  • Not directly. Event if you injected the "knp_menu.renderer.twig" service (the "Knp\Menu\Renderer\TwigRenderer" class above) in your menu builder, there are no mutators for default options. You could eventually implement some mutators yourself by overriding the renderer class with the parameter "knp_menu.renderer.twig.class" – Lionel Gaillard Oct 27 '17 at 13:37
21

You can pass it like that:

{{ knp_menu_render('AcmeDemoBundle:Builder:mainMenu', {'currentClass': 'active'}) }}
Bartek
  • 1,349
  • 7
  • 13