【问题标题】:How to have dynamic navigation items in a TWIG template using ZF2 and ZfcTWIG如何使用 ZF2 和 ZfcTWIG 在 TWIG 模板中添加动态导航项
【发布时间】:2013-03-30 07:00:43
【问题描述】:

早安,

我不知道如何在 ZF2 中实现我想要的,因为我是新手。我有一个使用 ZF2 和 ZfcTwig 模块的站点,我希望我的 TWIG 模板具有基于 mysite.global.php 中的设置的动态导航。我希望它读取我的配置数组的内容并使用这些内容创建主导航,这将应用于我网站的所有页面。

我已经创建了 layout.twig:

<div class="nav-collapse collapse">
   <ul class="nav">
   {% for item in navigation %}

    <li {% if item.active %} class="active" {% endif %}>
       <a href="{{ url(item.route) }}">
       {{ translate(item.name) }}
       </a>
    </li>

   {% endfor %}
   </ul>
</div><!--/.nav-collapse -->

还有我的 mysite.global.php

return array(
    'mysite' => array(
        'navigation' => array(
            'home' => array(
               'name' => 'Home',
               'route' => 'home',
             ),
            'profile' => array(
               'name' => 'Profile',
               'route' => 'myroute',
               'active' => true
            ),
        )
    )
);

这就是我迷路的地方。如何将我的设置转移到我的 layout.twig 以便使用布局的每个页面都使用相同的项目?我尝试了这段代码并将其放在我的 Module.php 中的 onBootstrap() 方法中:

//get my settings and place it to $navigation then

$view = $e->getApplication('application')->getMvcEvent()->getViewModel();

$view->navigation = $navigation;

但它不起作用。 {{ navigation }} 仍然包含一个空数组。我做错了什么?

【问题讨论】:

    标签: php navigation zend-framework2 twig template-engine


    【解决方案1】:

    我使用的解决方案:

    在module.config.php中

     'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
            'Navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
         ),
      ),
    
     .....
     'navigation' => array('default' =>array(
        array('label' => 'Home', 'route' => 'home', 'pages' => array(
                array('label' => 'Profile', 'route' => 'myroute'),
              ),
             ),
          ),
        ), 
    

    然后在 layout.twig 中

     {{  navigation('navigation').menu() | raw }}
    

    {{  navigation('navigation').breadcrumbs().render('navigation') | raw }}
    

    就是这样,它应该可以工作,顺便说一句,它使用Navigation helper

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多