0

I have a problem with customizing knp-menu template.

I simply extent the knp_menu.html.twig like in this site is explained.

If I try it like this I get this error:

enter image description here

My knp_menu.html.twig looked like this:

{% extends 'knp_menu.html.twig' %}

{% block linkElement %}{% import _self as knp_menu %}
   <a href="{{ item.uri }}"{{ knp_menu.attributes(item.linkAttributes) }}>{{ block('label') }}</a>
{% endblock %}

I try to render the template like this:

{{ knp_menu_render('MainShowBundle:Builder:mainUserMenu', {'template': 'MainShowBundle:Menu:knp_menu.html.twig'}) }}

I hope someone can help me.

Thanks. Michael

Community
  • 1
  • 1
Slowwie
  • 1,146
  • 2
  • 20
  • 36

3 Answers3

3

You need to import the macros from the parent template before you can use them.

{% block linkElement %}
    {% import 'knp_menu.html.twig' as knp_menu %}
    <a href="{{ item.uri }}"{{ knp_menu.attributes(item.linkAttributes) }}>
        <span>{{ block('label') }}</span>
    </a>
{% endblock %}


I already replied to this where you originally posted your question

D4R5C0D3
  • 91
  • 3
1

Have you already tried clearing the cache?

php app/console cache:clear

Javier Núñez
  • 612
  • 1
  • 5
  • 17
1

I am working on Sonata project which is Symfony based CMS like. In the admin area, I added custom template for the menu and in that template I did:

{% extends 'knp_menu.html.twig' %}

{% block linkElement %}
    {% import 'knp_menu.html.twig' as knp_menu %}
    <a href="{{ item.uri }}"{{ knp_menu.attributes(item.linkAttributes) }}>
        <span>{{ block('label') }}</span>
    </a>
{% endblock %}

{% block spanElement %}
{% import 'knp_menu.html.twig' as knp_menu %}
<a href="{{ item.uri }}"{ knp_menu.attributes(item.linkAttributes) }{ block('label') }</a>
{% endblock %}

Note that you might need to add "spanElement" as well as "linkElement".

Full template implementation https://github.com/sonata-project/sandbox-build/blob/2.4/vendor/knplabs/knp-menu/src/Knp/Menu/Resources/views/knp_menu.html.twig

Tareq
  • 5,283
  • 2
  • 15
  • 18