覆盖KnpMenu子菜单项


Override KnpMenu submenu items

我用KnpMenu制作了一个菜单,我正在尝试覆盖子菜单

这就是我添加子菜单项的方式

$menu
    ->addChild('sidebar.front.servers', ['route' => 'server_index'])
    ->setExtras([
        'icon'               => 'fa fa-hdd-o',
        'regex'              => '#^/servers/#',
    ])
;
$menu['sidebar.front.servers']
    ->addChild('nnanana', ['route' => 'server_index'])
;

我在knp_menu.html.twig上进行了搜索,以查找呈现子菜单的内容。

我找到了这个谁渲染子菜单列表和项目。

{% block list %}
    {% if item.hasChildren and options.depth is not same as(0) and item.displayChildren %}
        {% import _self as knp_menu %}
        <ul{{ knp_menu.attributes(listAttributes) }}>
            {{ block('children') }}
        </ul>
     {% endif %}
{% endblock %}
{% block children %}
    {# save current variables #}
    {% set currentOptions = options %}
    {% set currentItem = item %}
    {# update the depth for children #}
    {% if options.depth is not none %}
        {% set options = options|merge({'depth': currentOptions.depth - 1}) %}
    {% endif %}
    {# update the matchingDepth for children #}
    {% if options.matchingDepth is not none and options.matchingDepth > 0 %}
    {% set options = options|merge({'matchingDepth': currentOptions.matchingDepth - 1}) %}
    {% endif %}
    {% for item in currentItem.children %}
        {{ block('item') }}
    {% endfor %}
    {# restore current variables #}
    {% set item = currentItem %}
    {% set options = currentOptions %}
{% endblock %}

这将类放在子菜单列表中。

{%- set childrenClasses = item.childrenAttribute('class') is not empty ? [item.childrenAttribute('class')] : [] %}
{%- set childrenClasses = childrenClasses|merge(['menu_level_' ~ item.level]) %}
{%- set listAttributes = item.childrenAttributes|merge({'class': childrenClasses|join(' ') }) %}

这将呈现的所有子菜单项

{{ block('list') }}

但是当我试图覆盖我模板中的这个块时,比如这个

{% block item %}
    {% import 'knp_menu.html.twig' as knp_menu %}
    <a href="#">test</a>
{% endblock %}

这不起作用,菜单也不再渲染,我只显示了test。。。

我做了完全相同的覆盖每个菜单项和这项工作。

如何覆盖此子菜单?

感谢

我找到了覆盖子菜单项的方法。

子菜单项使用与主菜单项相同的代码进行渲染。

所以要覆盖它,只需添加一个像这样的分支条件,并在中做任何你想做的事情

{% if item.hasChildren and options.depth is not same as(0) and item.displayChildren %}
    <a href="#">
    <i class="{{ item.extra('submenu-icon') }}"></i>
{% else %}