php – 添加类到knp菜单根元素与枝
发布时间:2020-12-13 14:13:47 所属栏目:PHP教程 来源:网络整理
导读:将类添加到knp_menu的根元素 ul中的正确方法是什么?有枝吗 我尝试了很多事情: 1. {{ knp_menu_render('main',{'class': 'foo'}) }} 2. {{ knp_menu_render('main',{'attributes': {'class': 'foo'}}) }} 3. {{ knp_menu_render('main',{'listAttributes':
将类添加到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' }) }} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |