将类添加到knp_menu的根元素< ul>中的正确方法是什么?有枝吗
我尝试了很多事情:
1.
- {{ knp_menu_render('main',{'class': 'foo'}) }}
2.
- {{ knp_menu_render('main',{'attributes': {'class': 'foo'}}) }}
3.
- {{ knp_menu_render('main',{'listAttributes': {'class': 'foo'}}) }}
4.
- {{ knp_menu_render('main',{'attributes': {'listAttributes': {'class': 'foo'}}}) }}
他们都没有工作
您可以将其添加到菜单生成器中,如
- $menu = $this->factory->createItem('root',array(
- 'childrenAttributes' => array(
- 'class' => 'foo',),));
更新
我只是收到一个关于这个的通知,并发现另一种方式,虽然它要求你使用自定义模板来实现它.
在您的自定义模板中,您需要覆盖列表块.
- {% 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值传递数组.通过将listAttributes.class(如果存在)作为listAttributes.class值加入option.rootClass(如果存在),则生成此属性.
使用{%set options = options | merge({‘rootClass’:”})%}将option.rootClass值重置为“’,以使其不会添加到每个子菜单中.
这将允许您使用..
- {{ knp_menu_render('main',{'rootClass': 'foo' }) }}