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');
}

Advertisements

3 responses to this post.

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

    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 3:37 am

      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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: