KnpMenuBundle - 发送选项并在 Twig 中使用它们 [英] KnpMenuBundle - send Options and use them in Twig

查看:32
本文介绍了KnpMenuBundle - 发送选项并在 Twig 中使用它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 KnpMenuBundle 在我的导航栏菜单上写了最后几天.我只想在途中给模板一些参数,然后对其做出反应.是否可以?我试过这个:

I wrote the last days on my nav-bar-menu whit KnpMenuBundle. I want simply to give the template a few parameters on the way and then react on it. Is it possible? I tried this:

$menu->addChild('Registration', array('route' => 'fos_user_registration_register',
                                      'icon'  => array('glyphicon' => 'briefcase')));

然后我想在 Twig 模板中选择它:

And then I want to pick this in Twig template:

{% if icon['glyphicon'] is defined %}
    <span class="glyphicon glyphicon-{{icon['glyphicon']}} "></span>
{% endif %}

我尝试了很多,但没有任何效果.接下来我可以尝试什么?

I tried a lot, but nothing works. What can I try next?

推荐答案

我是这样做的

菜单类:

$menu->addChild('Home', array(
    'route' => 'home',
    'extras' => array('icon' => 'home')
));

我在模板中调用菜单,我需要一个特定的树枝模板

My menu call in the template, i ask for a specific twig template

{{ knp_menu_render('MyBundle:Menu:primaryNav', {'template': 'MyBundle:Menu:primaryNav.html.twig'}) }}

在树枝模板中,我复制/粘贴 knpmenubundle 模板并编辑一些块.您也可以使用扩展"和模板的继承内容(可能是更好的主意)来做到这一点.

In the twig template, i copy/paste the knpmenubundle template and edit some blocks. you may be able to do this with 'extends' and template's inheritance stuffs as well (probably a better idea).

图标编辑块示例:

{% block spanElement %}
<a href="#" class="dropdown-toggle">
    {% if item.extras.icon is defined %}<i class="icon-{{ item.extras.icon }}"></i>{% endif %}
    <span class="menu-text"> {{ block('label') }}</span>
    <b class="arrow icon-angle-down"></b>
</a>
{% endblock %}

您可能需要的是菜单类中的这个附加"字段:)

What you needed was probably this "extras" field in the menu class :)

这篇关于KnpMenuBundle - 发送选项并在 Twig 中使用它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆