簡體   English   中英

Zend2導航-渲染1級分支頭+子級(如果處於活動狀態)

[英]Zend2 Navigation - Render Level 1 Branch Head + Children if active

我在zend2中創建了一個菜單,到目前為止該菜單仍然有效。 完美。

- 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();
?>

任何幫助將非常感激!

您可以使用自定義視圖模板並調用renderPartial()方法,甚至更好地擴展Zend\\View\\Helper\\Navigation\\Menu來做到這一點。

基本上,僅對於非活動分支,必須使用選項$maxDepth = 0調用父renderMenu()

您可以使用很多方法,例如Zend\\View\\Helper\\Navigation\\AbstractHelper::findActive()

如irc上#zftalk上所述。...您缺少將菜單助手設置為僅呈現活動分支的信息,文檔位於此處: http : //framework.zend.com/manual/2.2/en/modules/zend .navigation.view.helper.menu.html


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

應該解決你的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM