2

In my layout script, I need too generate / render my menu.

If the menu item have a submenu I would change my menu item so it will render <li class="submenu">

The reason is that i would have an image on <li> element if subpages exits!

<ul> 
    <li> 
        <a href="/da/front/news">Nyt</a> 
    </li> 
    <li class="submenu"> 
        <a href="/da/front/events">Aktiviteter</a> 
        <ul"> 
            <li> 
                <a href="/da/front/document/get/document/barserves-2010-2/doctype/html">Barvagt</a> 
            </li> 
            <li> 
                <a href="/da/front/events/history">Afsluttede aktiviteter</a> 
            </li> 
        </ul> 
    </li> 
<ul>

this is part of my layout script

<?php
$config = new Zend_Config_Xml ( APPLICATION_PATH . '/configs/navigation/front.xml' );
$container = new Zend_Navigation ( $config );
$this->navigation($container);
echo $this->navigation()->menu()->render(); 

3 Answers 3

4

Found a solution

My layout file

<?php
global $config;
$menuconfig = new Zend_Config_Xml ( $config->navigation->file );
$container = new Zend_Navigation ( $menuconfig );
$this->navigation($container);
$partial = array('menu.phtml','front');
$this->navigation()->menu()->setPartial($partial);
echo $this->navigation()->menu()->render(); 
?>

And my partial file

<?php

$html = array ();

$iterator = new RecursiveIteratorIterator ( $this->container, RecursiveIteratorIterator::SELF_FIRST );
$prevDepth = - 1;
foreach ( $iterator as $page ) {
    $depth = $iterator->getDepth ();
    $isActive = $page->isActive ( true );
    if ($depth > $prevDepth) {
        $html [] = '<ul>' . "\n";
    } else if ($prevDepth > $depth) {
        for($i = $prevDepth; $i > $depth; $i --) {
            $html [] = '</li>' . "\n";
            $html [] = '</ul>' . "\n";
        }
        $html [] = '    </li>' . "\n";
    } else {
        $html [] = '    </li>' . "\n";
    }
    if ($page->haspages ()) {
        $liClass = $isActive ? ' class="active submenu"' : ' class="submenu"';
    } else {
        $liClass = $isActive ? ' class="active"' : '';
    }
    $html [] = '<li' . $liClass . '>' . "\n";
    $html [] = '<a href="' . $page->getHref () . '">' . $page->getLabel () . '</a>' . "\n";
    $prevDepth = $depth;
}
echo join ( PHP_EOL, $html );
Sign up to request clarification or add additional context in comments.

1 Comment

Please don't use "global". Sooner or later you will be in trouble when you use global variables.
2

You can add a css class or an id to a navigation item. It looks like you're navigation is generated from xml so:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <navigation>
        <articles>
            <label>Articles</label>
            <class>submenu</class>
             <controller>articles</controller>
             <action>index</action>
            <pages>
                <example>
                    <label>example</label>
                    <id>page1</id>
                    <controller>articles</controller>
                    <action>index</action>
                </example>
             </pages> 
        </articles>     
    </navigation>
</config>

Failing that you could extend Zend_Navigation

Comments

0

Is this for css? Why not just use a wrapping div called 'navigation' and then go:

div#navigation ul li ul

2 Comments

My <li> section need a image if subpages exits
The best code found on the net for Zend Menu. Thanks Tirsvad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.