dousiyou1058 2013-03-30 07:00
浏览 38
已采纳

如何使用ZF2和ZfcTWIG在TWIG模板中创建动态导航项

Good day,

I am lost on how to implement what I want in ZF2 since I'm new to it. I have a site that uses ZF2 and ZfcTwig module and I want my TWIG template to have a dynamic navigation based on the settings placed in my mysite.global.php. I want it to read the contents of my config array and create the main navigation using the contents and this will be implemented to all pages of my site.

I already created my 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 -->

and my mysite.global.php

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

This is where I'm lost. How can I transfer my settings to my layout.twig so that every page that uses the layout will use the same items? I tried this code and placed it to my Module.php in the onBootstrap() method:

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

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

$view->navigation = $navigation;

but it doesn't work. {{ navigation }} still contains an empty array. What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • douchai7891 2013-03-30 23:39
    关注

    The solution which I used:

    in 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'),
              ),
             ),
          ),
        ), 
    

    And then in layout.twig

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

    and

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

    And that's all, it should work and by the way, it uses Navigation helper

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看