zend-frameworkzend-navigation

Zend_Navigation rendering submenu with partial


I've posted an edit to my question. While working on it I noticed the problem is easy to simplify. I need a custom format of my submenu so i have to use partial. But then the problem occurs.

The below code shows the INCORRECT level (0):

            echo $this->navigation()->menu()
                ->setMinDepth(1)
                ->setMaxDepth(1)
                ->setRenderParents(false)
                ->setOnlyActiveBranch(true)
                ->renderPartial(null, array('partials/menu.phtml', 'default'));

The below code shows the CORRECT menu level (1)

            echo $this->navigation()->menu()
                ->setMinDepth(1)
                ->setMaxDepth(1)
                ->setRenderParents(false)
                ->setOnlyActiveBranch(true)
                ->render();

Any ideas? Guys please. I would appreciate any help!

Edit

My partials/menu.phtml:

    foreach ($this->container as $page) 
{
    $active = $page->isActive();
    echo '<div class="item">';
        echo '<a class="'. ($active ? 'active' : '') .'" href="' . $this->baseUrl($page->getHref()) . '">' . $page->getLabel() . '</a>';
    echo '</div>';
}

EDIT 2

My understanding of Zend_Navigation was, first to prepare container and than put it through partial.

            $nav = $this->navigation()->menu()->setOnlyActiveBranch(true)->getContainer();
        echo $this->navigation()->menu()->renderPartial($nav, array('/partials/menu.phtml', 'default'));

What is the point of setting set{Min/Max}Depth, parentRendering at the container when passing it anywehere is useless?


Solution

  • I use this code:

    <?=$this->navigation()->menu()->renderPartial(null, 'shared/menu.phtml')?>
    

    you should pass true to the method $page->isActive(true) so that also functions in depth.

    in your partial

    foreach ($this->container as $page) {
      $active = $page->isActive(true);
      if (count($page->getPages())) {
        foreach ($page->getPages() as $subPage) {
          $active = $subPage->isActive(true);
          echo '<div class="item">';
            echo '<a class="'. ($active ? 'active' : '') .'" href="' . $this->baseUrl($subPage->getHref()) . '">' . $subPage->getLabel() . '</a>';
          echo '</div>';      
        }
      }
    }
    

    before the second foreach you could add a check if and when to show the submenu.

    my 2 cent.

    EDIT

    try this:

    $partial = array('partials/menu.phtml', 'default');
    echo $this->navigation()->menu()
      ->setMinDepth(1)
      ->setMaxDepth(1)
      ->setRenderParents(false)
      ->setOnlyActiveBranch(true)
      ->setPartial($partial)
      ->render();