[英]KnpMenuBundle - how can i set an icon class to each elements of menu?
I want to my view code looks like this: 我希望我的视图代码看起来像这样:
<li>
<a href="path/to/action">
<i class="icon-class"></i>
<span class="title">Title</span>
</a>
</li>
I create menu elements by Menu Builder: 我通过Menu Builder创建菜单元素:
class Builder extends ContainerAware
{
public function adminMenu(FactoryInterface $factory, array $options)
{
$menu = $factory->createItem('root');
$menu->addChild('Dashboard', array(
'route' => 'admin_dashboard',
));
return $menu;
}
}
I have overwritten view with following code (knp_menu.html.twig): 我用以下代码覆盖了视图(knp_menu.html.twig):
{% block linkElement %}
{% import _self as knp_menu %}
<a href="{{ item.uri }}"{{ knp_menu.attributes(item.linkAttributes) }}>
<i class="icon-class"></i>
<span class="title">{{ block('label') }}</span>
</a>
{% endblock %}
How can i pass a value of icon class name to <i>
element in method adminMenu(), in Builder class? 如何在Builder类中的方法adminMenu()中将图标类名的值传递给
<i>
元素? What is the most simple way to do it? 最简单的方法是什么?
You can add any attribute you want with 您可以添加所需的任何属性
$menu->addChild('Dashboard', array(
'route' => 'admin_dashboard',
))->setAttribute('icon', 'icon-class');
then 然后
{{ item.attribute('icon') }}
I'd suggest the use of extras
instead of attributes
, because attributes
is used to render the li
element's attributes. 我建议使用
extras
而不是attributes
,因为attributes
用于呈现li
元素的属性。
$menu->addChild('Dashboard', array(
'uri' => '#',
))->setAttribute('icon', 'icon-class');
and 和
{{ item.attribute('icon') }}
will probably be rendered as: 可能会呈现为:
<li icon="icon-class"><a href="#"><i class="fa fa-icon-class" aria-hidden="true"></i> Dashboard</a></li>
Whereas: 鉴于:
$menu->addChild('Dashboard', array(
'uri' => '#',
))->setExtra('icon', 'icon-class');
and 和
{{ item.extra('icon') }}
Will probably just be rendered as: 可能只会呈现为:
<li><a href="#"><i class="fa fa-icon-class" aria-hidden="true"></i> Dashboard</a></li>
Also see this answer: https://stackoverflow.com/a/19095287/2106834 . 另请参阅此答案: https : //stackoverflow.com/a/19095287/2106834 。
Simply: 只是:
$menu->addChild('Home', ['route' => 'home_page'])
->setAttribute('icon', 'fa fa-home');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.