KnpMenuBundle - send Options and use them in Twig

后端 未结 2 837
慢半拍i
慢半拍i 2021-01-21 23:30

I wrote the last days on my nav-bar-menu whit KnpMenuBundle. I want simply to give the template a few parameters on the way and then react on it. Is it possible? I tried this:

2条回答
  •  一向
    一向 (楼主)
    2021-01-22 00:10

    Here is how i did it

    The Menu class:

    $menu->addChild('Home', array(
        'route' => 'home',
        'extras' => array('icon' => 'home')
    ));
    

    My menu call in the template, i ask for a specific twig template

    {{ knp_menu_render('MyBundle:Menu:primaryNav', {'template': 'MyBundle:Menu:primaryNav.html.twig'}) }}
    

    In the twig template, i copy/paste the knpmenubundle template and edit some blocks. you may be able to do this with 'extends' and template's inheritance stuffs as well (probably a better idea).

    Example of edited block for icon :

    {% block spanElement %}
    
        {% if item.extras.icon is defined %}{% endif %}
         {{ block('label') }}
        
    
    {% endblock %}
    

    What you needed was probably this "extras" field in the menu class :)

提交回复
热议问题