微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 添加类到knp菜单根元素与枝

将类添加到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' }) }}

原文地址:https://www.jb51.cc/php/130619.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐