symfony 3中的几个knp菜单模板

时间:2016-04-23 11:10:33

标签: symfony knpmenubundle

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

中的模板

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

1 个答案:

答案 0 :(得分:0)

如果在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;
    }
}