symfony 3 中的几个 knp 菜单模板

Several knp menu templates in symfony 3

有没有办法使用多个菜单模板,而不是 vendor/knplabs/knp-menu/src/Knp/Menu/Resources/views/knp_menu。html.twig

或者在Builder中为某些菜单设置模板名称(使用http://symfony.com/doc/current/bundles/KnpMenuBundle/index.html中的示例)?

如果在 knp_menu_render 函数中我们将调用模板 AppBundle:Menu:knp_menu.html.twig 然后将演示菜单文件移动到 src/AppBundle/Resources/views/Menu/knp_menu.html.twig,就像文档中的此处一样:http://symfony.com/doc/current/book/templating.html#template-naming-and-locations

按步骤:

在模板中写入:

{{ knp_menu_render('AppBundle:Builder:mainMenu', 'template': 'AppBundle:Menu:knp_menu.html.twig'}) }}

复制文件:

cp vendor/knplabs/knp-menu/src/Knp/Menu/Resources/views/knp_menu.html.twig src/AppBundle/Resources/views/Menu/knp_menu.html.twig

src/AppBundle/Menu/Builder 的示例。php

<?php
namespace AppBundle\Menu;

use Knp\Menu\FactoryInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;

class Builder implements ContainerAwareInterface
{
    use ContainerAwareTrait;

    public function mainMenu(FactoryInterface $factory, array $options)
    {
        $menu = $factory->createItem('root');

        $menu->addChild('Main', array('route' => 'homepage'));
        $menu->setChildrenAttribute('class', 'nav navbar-nav');

        // create another menu item
        $menu->addChild('About', array('route' => 'About'));
        $menu['About']->addChild('Contacts', array('route' => 'About'));
        $menu['About']->addChild('Contacts1', array('route' => 'About'));
        $menu['About']->setChildrenAttribute('class', 'dropdown-menu');
        // ... add more children

        return $menu;
    }
}