Zend Navigation. Submenu with custom option

2019-04-12 23:56发布

问题:

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(); 

回答1:

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


回答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



回答3:

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

div#navigation ul li ul