Zend_Navigation Render the SubMenu for Active Menu Item using a Partial

One of the biggest problems with Zend_Navigation helpers is using partials to render sub-menus in order to achieve customized rendering especially when additional properties have been defined.

The approach to the solution is as follows:


// find the first active menu item
$subnav = $this->navigation()->menu()->findOneByActive(1);
// The finder methods return null for no matches so nee to check that
if ($subnav != null) {
// render the matched menu using a partial
echo $this->navigation()->menu()->renderPartial($subnav, 'index/customsubmenu.phtml');
}

Advertisement

3 responses to this post.

  1. Posted by dbq on October 16, 2012 at 03:28

    Hello.
    Will you tell me the reason the below code doesn’t work and how to improve it?

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

    Like

    Reply

    • Posted by dbq on October 16, 2012 at 03:37

      Sorry, I mean how to use findOneByActive(…) to obtain the same as using set{Min/Max}Depth()?

      Like

      Reply

    • Thomas,

      I am not sure why your code may not be working, as the way the actual data is set-out may be an issue. My approach is to have unique top-level menus, to which all matching is done with the child menus being duplicated across the different parent menus as the need arises.

      You need to check your data and the way the menus are defined, hope this helps

      Stephen

      Like

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: