Rendering Active Branch of Zend Navigation Without

2020-03-29 02:53发布

I am rendering the top-level elements of a Zend Navigation object in one place like this:

echo $this->navigation()->menu()->setMaxDepth(0);

How do I render the navigation tree from the second level on down for the active branch? I've tried creating a partial that loops the $this->container object, but I don't know how to determine if my current item is the active branch. Once I've determined that it's the active branch how do I render the menu? Am I doing this the hard way and missing something obvious?

Thanks!


UPDATE:

I accepted a solution because that's what I used, but I also would like to provide the answer to my actual question, for reference sake. ($this is the view object)

// Find the active branch, at a depth of one
$branch = $this->navigation()->findActive($this->nav, 1, 1);
if (0 == count($branch)) {
    // no active branch, find the default branch
    $pages = $this->nav->findById('default-branch')->getPages();
} else {
    $pages = $branch['page']->getPages();
}
$this->subNav = new Zend_Navigation($pages);

$this->subNav can then be used to render the sub-menu.

3条回答
相关推荐>>
2楼-- · 2020-03-29 03:17

If I got your question right, this is how I do it:

print $this->navigation()->menu()->renderMenu(null, array(
    'minDepth' => 1,
    'maxDepth' => 1,
    'onlyActiveBranch' => true,
    'renderParents' => false));

Renders only the submenu of currently active menu.

查看更多
乱世女痞
3楼-- · 2020-03-29 03:20

I do it this way:

<?php

// Render top-level elements
echo $this->navigation()->menu()->setMaxDepth(0);

// Render 2nd level elements for active element
echo $this->navigation()->menu()
        ->setOnlyActiveBranch(true)
        ->setRenderParents(false)
        ->setMinDepth(1);

?>

but this is not a good solution. Better one for each level as a separate menu:

<!-- level 1 -->
<?php echo $this->navigation()->menu()->setMaxDepth(0); ?>


<!-- level 2 -->
<?php echo $this->navigation()->menu()->setOnlyActiveBranch(true)->setRenderParents(true)->setMinDepth(1)->setMaxDepth(1); ?>



<!-- level 3 -->
<?php echo $this->navigation()->menu()->setOnlyActiveBranch(true)->setRenderParents(false)->setMinDepth(2)->setMaxDepth(2); ?>
查看更多
Luminary・发光体
4楼-- · 2020-03-29 03:22

I do something similar. My main navigation is handled with something like this...

$this->navigation()->menu()->setPartial('tabs.phtml');
echo $this->navigation()->menu()->render();

Then in my tabs.phtml I iterate over the container like so...

if (count($this->container)) {
  foreach($this->container as $page) {
    if ($page->isVisible()) {
      if ($page->isActive(true)) {
        $subcontainer = $page->getPages();
        foreach($subcontainer as $subpage) {
          // echo my link
        }
      }
    }
  }
}

I hope that helps a bit.

查看更多
登录 后发表回答