phpsymfonyoverridingknpmenubundle

Override options in knp menu bundle for symfony2


From the default knp menu bundle template:

{%- elseif matcher.isAncestor(item, options.matchingDepth) %}
    {%- set classes = classes|merge([options.ancestorClass]) %}

options.ancestorClass is equal to 'current_ancestor'. Is there a way to override this? I dont want to copy the wohle block item code which covers 50 lines of code, from which I only need to change one value.


Solution

  • 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);
    

    Try

    {{ knp_menu_render('AcmeDemoBundle:Builder:mainMenu', {'ancestorClass': 'your-class'}) }}
    

    From this link