0

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

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

4

1 回答 1

1

如果在 knp_menu_render 函数中,我们将调用模板 AppBundle:Menu:knp_menu.html.twig 然后将演示菜单文件移动到 src/AppBundle/Resources/views/Menu/knp_menu.html.twig 就像在文档中一样:http ://symfony.com /doc/current/book/template.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;
    }
}
于 2016-04-26T05:41:01.713 回答