Php 使用细枝将类添加到knp菜单根元素
使用twig将类添加到Php 使用细枝将类添加到knp菜单根元素,php,symfony,twig,knpmenubundle,Php,Symfony,Twig,Knpmenubundle,使用twig将类添加到knp\U菜单的根元素的正确方法是什么 我尝试了很多东西: 一, 二, 三, 四, 它们都不起作用您可以像..一样将其添加到菜单生成器中 $menu = $this->factory->createItem('root', array( 'childrenAttributes' => array( 'class' => 'foo', ), )); 更新 我刚刚收到一个关于这个的通知,并找
knp\U菜单的
根元素
的正确方法是什么
我尝试了很多东西:
一,
二,
三,
四,
它们都不起作用您可以像..一样将其添加到菜单生成器中
$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 %}
还未找到干净的解决方案来传递视图中的参数。我在
builder
类中的解决方案:
$menu->setChildrenAttribute('id','boo')
->setChildrenAttribute('class','foo');
尝试
{% set menu = knp_menu_get('AppBundle:Builder:categoriesMenu', [], {'childrenAttributes': {'class': 'menu'}}) %}
{{ knp_menu_render(menu) }}
在这里找到这是我唯一能做到的方法吗?不是作为一个参数?我不确定是否诚实,对不起。我会投票给你的答案,但不会接受它,因为这不是我想要的解决方案…请你提供一些关于如何创建此模板文件以及如何使用它的更多信息?我不能让它工作。tnx这是很久以前的事了,所以我不能确定,但我认为您需要在
knp\u菜单config的twig.template
下设置模板位置。请参见,您也可以使用$menu->setChildrenAttributes(['id'=>'myId','class'=>'myClass'])代码>设置多个属性的方法。问题是在模板中而不是生成器中的类中设置的
{{ 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_render('main', {'rootClass': 'foo' }) }}
{% set menu = knp_menu_get('AppBundle:Builder:categoriesMenu', [], {'childrenAttributes': {'class': 'menu'}}) %}
{{ knp_menu_render(menu) }}
{% set menu = knp_menu_get('AppBundle:Builder:mainMenu', []) %}
{% do menu.setChildrenAttribute('class', 'child-class') %}
{% knp_menu_render(menu, {'currentClass': 'active'}) %}