有没有办法在 KNP 菜单包中呈现自定义属性,如下所示:
$menu = $factory->createItem(Role::ROLE_PROGRAM_EVENT_PLANNER, array(
'route' => 'show_form_events',
'attributes' => array('class' => 'menu pe_planner'),
'extra' => array(
'content' => 'my custom content'
)
));
我通过在 a 标签后添加额外的 div 覆盖了 linkElement。在那个 div 中,我想呈现额外的内容
{% block linkElement %}
{% import _self as knp_menu %}
<a href="{{ item.uri }}"{{ knp_menu.attributes(item.linkAttributes) }}>{{ block('label') }}</a>
{% if item.hasChildren == false %}
<div class="custom">{{ item.getExtra('content') }}</div>
{% endif %}
{% endblock %}
最佳答案
实际上我今天也必须做同样的事情 ;)
菜单生成器
$menu->addChild(
'Dashboard',
array(
'route' => 'dashboard',
'attributes' => array(
'class' => 'navigation-entry'
),
'extras' => array(
'icon' => '6'
)
)
);
菜单模板
{% block linkElement %}
{% import "knp_menu.html.twig" as macros %}
<a href="{{ item.uri }}"{{ macros.attributes(item.linkAttributes) }}>
<span class="icon">{{ item.getExtra('icon') }}</span>
<span class="entry">{{ block('label') }}</span>
</a>
{% endblock %}
不要混淆图标内容,因为我使用的是图标字体。
关于symfony - 渲染自定义属性 KNP 菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21301710/