Render custom attribute KNP Menu

风流意气都作罢 提交于 2019-12-12 13:19:20

问题


Is there a way to render a custom attribute in the KNP Menu Bundle, something like this:

$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'
    )
));

I have overriden the linkElement by adding an extra div after the a-tag. In that div I would like to render extra content

{% 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 %}

回答1:


Actually i had to do quite the same for today ;)

MenuBuilder

$menu->addChild(
  'Dashboard',
  array(
    'route'      => 'dashboard',
    'attributes' => array(
      'class' => 'navigation-entry'
    ),
    'extras' => array(
      'icon' => '6'
    )
  )
);

menuTemplate

{% 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 %}

Dont be confused be the icon content because i use an icon font.



来源:https://stackoverflow.com/questions/21301710/render-custom-attribute-knp-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!