Aug
16
2009
Part 2: Rendering the sub menus (relevant to the active tab).
This is where we employ another trick because the Zend_Navigation menu view helper does provide an option to render the active menu, but it is meant literally not the active branch of the menu like we’re trying to achieve.
In the layout we need to get the root level pages then we assume the 1st one is Home and root of all other navigation items. After this we loop through the pages below Home to find the active one and set this as the $nav2Container which we’ll use next.
layout.phtml
// find the active page/container under the rootPage (Home) for nav2 menu
$rootPage = current($this->navigation()->getContainer()->getPages());
$pages = $rootPage->getPages();
foreach($pages as $page) {
if($page->isActive(true)) {
$nav2Container = $page;
}
}
Now that we know what the root of this sub menu should be we can use the helpers to render it (this is also in your layout file).
if(isset($nav2Container)) {
echo $this->navigation()->menu()->renderMenu($nav2Container,array('ulClass' => 'nav2'));
};
1 comment | tags: sub menu, tab, Zend_Navigation | posted in Zend Framework
Aug
16
2009
It is some what tricky with the Zend_Navigation menu helpers to set up your typical tab-based navigation with sub pages relevant to the tab your on, as seen below. But it’s not far difficult so don’t be put off, hopefully the view helpers will improve over time.

In the mean time this is a 3 part series in achieving the navigation I set up in utilitiesman.com as seen above.
Part 1: Setting up the navigation, and rendering the tabs.
Set up your navigation configuration, I’ve done this in xml.
< ?xml version="1.0" encoding="UTF-8"?>
<configdata>
<nav>
<label>Home</label>
<module>default</module>
<controller>index</controller>
<action>index</action>
<pages>
<computers>
<label>Computers</label>
<module>computers</module>
<controller>index</controller>
<action>index</action>
<pages>
<networking>
<label>Networking</label>
<module>computers</module>
<controller>networking</controller>
<action>index</action>
<pages>
<ipaddress>
<label>IP Address</label>
<module>computers</module>
<controller>networking</controller>
<action>ipaddress</action>
</ipaddress>
<ping>
<label>Ping</label>
[... code removed for example ...]
</ping>
[... code removed for example ...]
</pages>
</networking>
<security>
[... code removed for example ...]
</security>
</pages>
</computers>
<measures>
[... code removed for example ...]
</measures>
</pages>
</nav>
</configdata>
In your bootstrap file you’ll need setup the navigation and load your navigation config:
$navigationConfig = new Zend_Config_Xml('./application/Config/navigation.xml');
$navigation = new Zend_Navigation($navigationConfig);
Assuming you’re making use of the Zend_Layout this would go in your template
$this->navigation()->menu()->setPartial(array('nav1.phtml','default'));
echo $this->navigation()->menu()->render();
This is my partial menu template which is where some minor trickery is performed to see the Home page and first level pages in a single <ul> for easy application of CSS to make the <li>’s look like tabs. This is the “nav1.phtml” referred to in the layout.
echo '<ul id="nav1">';
// loop root level (only has Home, but later may have an Admin root page?)
foreach ($this->container as $page) {
// check if it is active (not recursive)
$isActive = $page->isActive(false);
$liClass = $isActive ? ' class="active"' : '';
echo '<li ' . $liClass . '>' . $this->menu()->htmlify($page) . '</li>', PHP_EOL;
// loop next level
foreach ($page as $page) {
// check if it is active (recursive)
$isActive = $page->isActive(true);
$liClass = $isActive ? ' class="active"' : '';
echo '<li ' . $liClass . '>' . $this->menu()->htmlify($page) . '</li>', PHP_EOL;
}
}
echo '</ul>';
What the partial template above renders is something like this, neatly all in a single hieratical level..
<ul id="nav1">
<li><a ...>Home</a></li>
<li class="active"><a ...>Computers</a></li>
<li><a ...>Measures</a></li>
</ul>
4 comments | tags: navigation, tabs, Zend_Navigation | posted in Zend Framework
Aug
16
2009
The most logical place to render your breadcrumb from is your master layout… However it’s unlikely you’ll ever want to render to on your home page right? I mean what is the point of seeing just “Home”.
This can be simply achieved using a partial template to render your breadcrumb. The code below should explain things for you, but leave a comment if you would like further explanation.
bootstrap.php
$navigationConfig = new Zend_Config_Xml('./application/config/navigation.xml');
$navigation = new Zend_Navigation($navigationConfig);
navigation.xml
< ?xml version="1.0" encoding="UTF-8"?>
<configdata>
<nav>
<label>Home</label>
<module>default</module>
<controller>index</controller>
<action>index</action>
<pages>
<computers>
<label>Computers</label>
<module>computers</module>
<controller>index</controller>
<action>index</action>
<pages>
<networking>
<label>Networking</label>
<module>computers</module>
<controller>networking</controller>
<action>index</action>
<pages>
[...]
</pages>
</networking></pages></computers></pages></nav>
</configdata>
layout.phtml
echo $this->navigation()->breadcrumbs()->setPartial(array('breadcrumb.phtml', 'default'));
breadcrumb.phtml
// only render if there is more than 1 page in the breadcrumb
if (count($this->pages) > 1) {
$links = array();
foreach ($this->pages as $page) {
$links[] = $this->breadcrumbs()->htmlify($page);
}
echo implode(' > ', $links);
}
1 comment | tags: breadcrumb, breadcrumbs, home page, sub pages, Zend_Navigation, Zend_View_Helper_Navigation_Breadcrumbs | posted in Zend Framework