【问题标题】:Render custom attribute KNP Menu渲染自定义属性 KNP 菜单
【发布时间】:2014-01-23 07:15:34
【问题描述】:

有没有办法在 KNP Menu Bundle 中呈现自定义属性,如下所示:

$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-tag 后添加一个额外的 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 %}

【问题讨论】:

  • 您的代码是否出错?在我的记忆中,我会说我以类似的方式做到了这一点

标签: symfony knpmenubundle knpmenu


【解决方案1】:

其实我今天也要做同样的事情;)

菜单生成器

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

不要对图标内容感到困惑,因为我使用的是图标字体。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多