更新时间:2022-03-29 02:53:46
您可以将其添加到菜单构建器中,例如..
You can add it in your menu builder like..
$menu = $this->factory->createItem('root', array(
'childrenAttributes' => array(
'class' => 'foo',
),
));
更新
我刚刚收到有关此问题的通知,并找到了另一种方法,尽管它需要您使用自定义模板来实现.
I just got a notification about this and found another way although it requires you to use a custom template to achieve it.
在您的自定义模板中,您需要覆盖 list
块,例如..
In your custom template you need to override the list
block like..
{% block list %}
{% if item.hasChildren and options.depth is not sameas(0) and item.displayChildren %}
{% import 'knp_menu.html.twig' as knp_menu %}
<ul{{ knp_menu.attributes(listAttributes|merge({'class': [
options.rootClass is defined ? options.rootClass : '',
listAttributes.class is defined ? listAttributes.class : ''
]|join(' ')
})) }}>
{% set options = options|merge({'rootClass': '' }) %}
{{ block('children') }}
</ul>
{% endif %}
{% endblock %}
在这个而不是使用 knp_menu.attributes(listAttributes)
中,您传入一个带有动态生成的 listAttributes.class
值的数组.该属性是通过将 option.rootClass
(如果存在)与 listAttributes.class
(如果存在)作为 listAttributes.class
值来生成的.
In this rather than use knp_menu.attributes(listAttributes)
you pass in a array with your on-the-fly generated listAttributes.class
value. This attribute is generate by joining option.rootClass
(if it exists) with listAttributes.class
(if it exists) as the listAttributes.class
value.
option.rootClass
值在使用后使用 {% set options = options|merge({'rootClass': '' }) %}
这样它就不会被添加到每个子菜单中.
The option.rootClass
value is reset to ''
after use using {% set options = options|merge({'rootClass': '' }) %}
so that it is not added to every sub-menu.
这将允许您使用...呈现菜单
This will allow you to render your menu using..
{{ knp_menu_render('main', {'rootClass': 'foo' }) }}