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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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' }) }}

脚本宝典总结

以上是脚本宝典为你收集整理的php – 添加类到knp菜单根元素与枝全部内容,希望文章能够帮你解决php – 添加类到knp菜单根元素与枝所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。