Symfony Knp Menu Extra Bundle

0.1.1 2020-01-09 11:30 UTC


more than work in progress

currently only the menu template is used:

add to config.yaml (knp_menu.yaml):

#    # use "twig: false" to disable the Twig extension and the TwigRenderer
        template: '@C33sKnpExtraMenu/menu.html.twig'


        template: '@C33sKnpMenuExtra/menu.html.twig'

add to bundles.php:

C33s\Bundle\KnpMenuExtraBundle\C33sKnpMenuExtraBundle::class => ['all' => true],

How to customize KNPMenuBundle?

I can't figure out how to add an image or a span tag using the KnpMenuBundle.

I simply want this:

         <img src="{{asset('bundles/mybundle/images/my_image.png')}} /">
         <span>My Title</span>        

In the MenuBuilder, this would start with:

$menu->addChild('My Title');

How could I add the image in the

statement? EDIT: THE EASY WAY

There is actually an easy way to do this within the bundle:

  1. Copy the template vendor\KnpMenu\src\Knp\Menu\Resources\views\knp_menu.html.twig into your Acme\AcmeBundle\Resources\views\Menu\knp_menu.html.twig and extend it as follow:
{% extends 'knp_menu.html.twig' %}
  1. Modify the template according to your needs. For example, if you decide to add a span tag each time you use $menu->addChild('Your Title');, simply add the span tag between :
{% block linkElement %}
    <a href="{{ item.uri }}"{{ _self.attributes(item.linkAttributes) }}>
        <span>{{ block('label') }}</span>
{% endblock %}
  1. You can now choose your custom layout when using the menu:
{{ knp_menu_render('main', {'template': 'AcmeBundle:Menu:knp_menu.html.twig'}) }}