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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)