简体   繁体   English

KnpMenuBundle - 如何为菜单的每个元素设置一个图标类?

[英]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.

相关问题 如何在 codeigniter 中将 class=&quot;active&quot; 设置为导航菜单? - How can I set class="active" to navigation menu in codeigniter? 如何在我的网站的下拉菜单中添加 FontAwesome 图标 class - How I can add FontAwesome icon class in dropdown menu to my website Symfony2 KnpMenuBundle:设置一个菜单项,即使它不在该菜单上 - Symfony2 KnpMenuBundle: set active a menu item even when its not on that menu 如何<a>使用 xpath 在 PHP 中</a>获取所有<a>具有类名的元素并添加一个</a><p><a>在每个元素下标记?</a> - How can I get all <a> elements with a class name in PHP using xpath and add a <p> tag under each of those elements? 如何将 class 添加到每个选项? - How can I add a class to each option? 如何使用PHP将“活动”类设置为菜单项? - How do I set an 'active' class to a menu item using PHP? 如何在 TCPDF 中将 img 和 a 元素相互放置? - How can I place img and a elements to each other in TCPDF? knpmenubundle:如何在构建器中获取用户数据? - knpmenubundle: How to get user data in builder? 如何为每个页面设置唯一的Cookie? - How can I set a unique cookie for each page? 使用KnpMenuBundle和Symfony3的菜单项的翻译参数 - Translation parameters for menu items using KnpMenuBundle and Symfony3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM