我已经用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显示.
我完全相同的做法,覆盖所有的菜单项和这项工作。
我怎样才能覆盖这个子菜单?
谢谢
发布于 2016-05-03 07:53:13
我找到了覆盖子菜单项的方法。
子菜单项以与主菜单项相同的代码呈现。
因此,要覆盖它,只需添加这样的小枝条件,并在里面做任何您想做的事情。
{% if item.hasChildren and options.depth is not same as(0) and item.displayChildren %}
<a href="#">
<i class="{{ item.extra('submenu-icon') }}"></i>
{% else %}https://stackoverflow.com/questions/36502038
复制相似问题