我正拼命地尝试在symfony-cmf中呈现一个子菜单。
示例
结构:
page1
├─p1-subpage1
├─p1-subpage2
└─p1-subpage3
page2
├─p2-subpage1
└─p2-subpage2每当当前页面位于page1层次结构中的某个位置时,它就应该使用p1-subpage*来呈现菜单,而当我在page2层次结构中时,它应该使用p2-subpage*来呈现菜单。从技术上讲,这意味着它应该将当前项设置为第一级的父级(如果它尚未在该级别上),并呈现一个节点级别(例如knp_menu_render('main', { depth: 1 }))。
问题可分为两部分:
思想与考验
getCurrentItem方法的,使用KNP-菜单2.0方法。尽管cmf 目前使用的1.1版,这是即将改变cmfMainContent变量,在{{ dump() }}中也找不到类似的东西(也没有包含菜单)。谢谢你的帮助。
发布于 2014-11-05 16:14:47
在这里查看一个使用选民来决定突出显示什么的例子:https://github.com/dbu/conference-tutorial-1.0/pull/20
除此之外,我们在KnpMenu 2.x兼容版本的MenuBundle上取得了很好的进展,但可能要到明年1月才能稳定发布(但我们可能会更早地发布一个版本)。):https://github.com/symfony-cmf/MenuBundle/pull/214
发布于 2014-11-06 10:08:08
昨天,我为自己的用例创建了一个包。
但是,由于我的所有页面共享相同的路径,您可能需要对其进行大量调整。
我仍然认为你可能会找到一些灵感,特别是在你的问题的第二部分。
我的捆绑:https://github.com/burki94/RecursiveMenuBundle/blob/master/README.md
AbstractRecursiveBuilder:https://github.com/burki94/RecursiveMenuBundle/blob/master/Menu/AbstractRecursiveBuilder.php
发布于 2014-11-06 15:45:15
这并不是一个真正的解决方案,因为它不符合我对兼容KnpMenu 2.*的要求。但是,这个不推荐的解决方案很简单:
{% set currentItem = knp_menu_get('main').currentItem %}
{% if currentItem is not null %}
{% if currentItem.getLevel() == 1 %}
{% set main = currentItem %}
{% else %}
{% set main = currentItem.getParent() %}
{% endif %}
{{ knp_menu_render(main, { 'template': 'ComBundle:Default:left_menu.html.twig', 'currentClass': 'uk-active' }) }}
{% endif %}https://stackoverflow.com/questions/26757539
复制相似问题