2013-06-05 24 views
0

我在zend2中創建了一個到目前爲止工作的菜單。完美。Zend2導航 - 渲染級別1分支頭+孩子如果活動

- Parent 1 
-- Child Page 1 
-- Child Page 2 
-- Child Page 3 
- Parent 2 
-- Child Page 1 
-- Child Page 2 
-- Child Page 3 
- Parent 3 
-- Child Page 1 
-- Child Page 2 
-- Child Page 3 

現在我只想在父活動時渲染子頁面。 比方說,父母2將積極渲染菜單是這樣的:

- Parent 1 
- Parent 2 
-- Child Page 1 
-- Child Page 2 
-- Child Page 3 
- Parent 3 

這是我global.php配置文件

return array(
    'navigation' => array(
     'default' => array(
      array(
       'label' => 'Parent 1', 
       'controller' => 'controllerone', 
       'pages' => array(
        array(
         'label' => 'Page1', 
         'controller' => 'controllerone', 
         'action' => 'page-one', 
        ), 
        array(
         'label' => 'Page2', 
         'controller' => 'controllerone', 
         'action' => 'page-two', 
        ), 
        array(
         'label' => 'Page3', 
         'controller' => 'controllerone', 
         'action' => 'page-one', 
        ) 
       ), 
      ), 
      array(
       'label' => 'Parent 2', 
       'controller' => 'controllertwo', 
       'pages' => array(
        array(
         'label' => 'Page1', 
         'controller' => 'controllertwo', 
         'action' => 'page-one', 
        ), 
        array(
         'label' => 'Page2', 
         'controller' => 'controllertwo', 
         'action' => 'page-two', 
        ), 
        array(
         'label' => 'Page3', 
         'controller' => 'controllertwo', 
         'action' => 'page-one', 
        ) 
       ), 
      ), 
      array(
       'label' => 'Parent 3', 
       'controller' => 'controllerthree', 
       'pages' => array(
        array(
         'label' => 'Page1', 
         'controller' => 'controllerthree', 
         'action' => 'page-one', 
        ), 
        array(
         'label' => 'Page2', 
         'controller' => 'controllerthree', 
         'action' => 'page-two', 
        ), 
        array(
         'label' => 'Page3', 
         'controller' => 'controllerthree', 
         'action' => 'page-one', 
        ) 
       ), 
      ), 
     ) 
    ), 
    'service_manager' => array(
     'factories' => array(
      'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory', 
     ), 
    ), 
); 

這就是我如何使我的佈局中導航

<?php 
echo $this 
    ->navigation('navigation') 
    ->menu(); 
?> 

任何幫助將不勝感激!

回答

-1

,張貼在#zftalk在IRC .... 你錯過了設置菜單助手只呈現活躍的分支,該文檔是在這裏: http://framework.zend.com/manual/2.2/en/modules/zend.navigation.view.helper.menu.html


<?php 
echo $this 
    ->navigation('navigation') 
    ->setOnlyActiveBranch(true) 
    ->menu(); 
?> 

應該解決您的問題

+0

我不認爲這解決了我的問題,因爲我需要渲染分支頭部,即使他們不活動。如果分支頭部活動,它的孩子也應該被渲染。 – n00b

0

您可以設法使用自定義視圖模板並調用renderPartial()方法,或者甚至更好地延伸Zend\View\Helper\Navigation\Menu

您基本上只能爲非活動分行撥打家長renderMenu()並選擇$maxDepth = 0

你可以使用很多方法,即Zend\View\Helper\Navigation\AbstractHelper::findActive()

+0

謝謝斯特凡諾,但正如我所說,我是一個血腥的新秀。你能舉個小例子嗎?幾行代碼就足以讓我們更好地理解代碼和東西的放置位置。 – n00b