KnpMenuBundle
KnpMenuBundle copied to clipboard
Attempted to call an undefined method named "macro_attributes" of class
Hi. I have problem with integration custom knpmenu template in Symfony 4.2. I still get this message: Attempted to call an undefined method named "macro_attributes" of class "__TwigTemplate_46b504049d303cbba60b9082144af113b977223be17dc1e45f9421ff99d7f8d3".
Can anybody help me?
This is my template:
{% extends 'knp_menu.html.twig' %}
{% block item %}
{% import _self as knpmenu %}
{% if item.displayed %}
{%- set attributes = item.attributes %}
{%- set is_group = attributes.group|default(false) %}
{%- set is_header = attributes.header|default(false) %}
{%- set divider_prepend = attributes.divider_prepend|default(false) %}
{%- set divider_append = attributes.divider_append|default(false) %}
{%- set class = is_header ? 'header' : 'item' %}
{# hide empty groups #}
{%- if not (is_group and item.hasChildren == false) -%}
{# unset bootstrap specific attributes #}
{%- set attributes = attributes|merge({'group': null, 'header': null, 'divider_prepend': null, 'divider_append': null, 'class': class }) %}
{#{%- if divider_prepend %}#}
{#{{ block('dividerElement') }}#}
{#{%- endif %}#}
{# building the class of the item #}
{%- set classes = item.attribute('class') is not empty ? [item.attribute('class')] : [] %}
{%- if matcher.isCurrent(item) %}
{%- set classes = classes|merge([options.currentClass]) %}
{%- elseif matcher.isAncestor(item, options.depth) %}
{%- set classes = classes|merge([options.ancestorClass]) %}
{%- endif %}
{%- if item.actsLikeFirst %}
{%- set classes = classes|merge([options.firstClass]) %}
{%- endif %}
{%- if item.actsLikeLast %}
{%- set classes = classes|merge([options.lastClass]) %}
{%- endif %}
{# building the class of the children #}
{%- set childrenClasses = item.childrenAttribute('class') is not empty ? [item.childrenAttribute('class')] : [] %}
{%- set childrenClasses = childrenClasses|merge(['menu_level_' ~ item.level]) %}
{# adding classes for group #}
{%- if is_group %}
{#{%- set classes = classes|merge([]) %}#}
{%- set childrenClasses = childrenClasses|merge(['nav nav-second-level']) %}
{%- endif %}
{# putting classes together #}
{%- if classes is not empty %}
{%- set attributes = attributes|merge({'class': classes|join(' ')}) %}
{%- endif %}
{%- set listAttributes = item.childrenAttributes|merge({'class': childrenClasses|join(' ') }) %}
{# displaying the item #}
<li{{ knpmenu.attributes(attributes) }}>
{%- if is_group %}
{{ block('groupElement') }}
{%- elseif is_header %}
{{ block('headerElement') }}
{%- elseif item.uri is not empty and (not item.current or options.currentAsLink) %}
{{ block('linkElement') }}
{%- else %}
{{ block('spanElement') }}
{%- endif %}
{# render the list of children#}
{{ block('list') }}
</li>
{%- if divider_append %}
{{ block('dividerElement') }}
{%- endif %}
{%- endif %}
{% endif %}
{% endblock %}
{% block dividerElement %}
{% if item.level == 1 %}
<li class="divider-vertical"></li>
{% else %}
<li class="divider"></li>
{% endif %}
{% endblock %}
{% block linkElement %}
<a href="{{ item.uri }}"{{ knpmenu.attributes(item.linkAttributes) }}>
{% if item.extra('icon') is not empty %}
<i class="{{ item.extra('icon') }} fa-fw"></i>
{% endif %}
<span> {{ block('label') }}</span>
</a>
{% endblock %}
{% block spanElement %}
<span {{ knpmenu.attributes(item.labelAttributes) }}>
{% if item.extra('icon') is not empty %}
<i class="{{ item.extra('icon') }} fa-fw"></i>
{% endif %}
{{ block('label') }}
</span>
{% endblock %}
{% block headerElement %}
<li class="nav-header">
<div class="dropdown profile-element">
<span {{ knpmenu.attributes(item.labelAttributes) }}>
{#{% if item.extra('icon') is not empty %}#}
{#<i class="{{ item.extra('icon') }} fa-fw"></i>#}
{#{% endif %}#}
{{ block('label') }}
</span>
</div>
</li>
{% endblock %}
{% block groupElement %}
{%- set classes = item.linkAttribute('class') is not empty ? [item.linkAttribute('class')] : [] %}
{%- set attributes = item.linkAttributes %}
{%- set attributes = attributes|merge({'class': classes|join(' ')}) %}
<a href="#"{{ knpmenu.attributes(attributes) }}>
{% if item.extra('icon') is not empty %}
<i class="{{ item.extra('icon') }} fa-fw"></i>
{% endif %}
<span class="nav-label">{{ block('label') }}</span> <span class="fa arrow"></span></a>
{% endblock %}
{% block label %}{{ item.label|trans }}{% endblock %}
Well, you use the knpmenu.attributes macro in many of your blocks. But only one of them imports the macro. I suspect that the error comes from another block.
Does it work if you import the macro in each block needing it ?
With importing in each block, it works