3

I'm trying to create a custom navigation for zend navigation but i have two questions:

  1. How do i pass variables to custom partial phtml, or if it's possible?
  2. How do i set a class trough the whole active menu tree?

This is my code so far:

in the controller:

$config = new Zend_Config($menu);
$nav = new Zend_Navigation();
$nav->addPages($config);
$this->view->nav = $nav;

in the view:

<?php echo $this->navigation($this->nav)->menu()->setPartial('menu.phtml')->render(); ?>

and my partial:

<?php

function genMenu($container)
{
    foreach ($container as $page)
    {
        echo '<li>';

        $href = $page->uri;
        $target = '_self';

        echo '<a href="' . $href . '" target="' . $target . '">' . $page->label . '</a>';

        if (!empty($page->pages))
        {
            echo '<ul>';

            genMenu($page->pages);

            echo '</ul>';
        }

        echo '</li>';
    }
}

echo '<ul>';

genMenu($this->container);

echo '</ul>';

Thanks everyone in advance!

MGP
  • 653
  • 1
  • 14
  • 33

2 Answers2

4
echo $this->navigation($this->nav)->menu()->setPartial('menu.phtml')->render(); ?>

is not quite correct, you have the right idea but try

//This will pass a valid container to your partial with the $this->nav
echo $this->navigation()->menu()->renderPartial($this->nav,'menu.phtml') ?>

here is the api:

public function renderPartial(Zend_Navigation_Container $container = null,
                                  $partial = null)

also this bit looks a little wonky as well:

$config = new Zend_Config($menu);
$nav = new Zend_Navigation();
$nav->addPages($config);
$this->view->nav = $nav;

I don't think ->addPages() is what you want here, I think what you need is:

//where $menu is the container and is config(.ini) object not .xml
//for xml use Zend_Config_Xml or Zend_Config_Json for JSON
$config = new Zend_Config($menu);
$nav = new Zend_Navigation($config);
//assign the container to the view
$this->view->nav = $nav;
RockyFord
  • 8,529
  • 1
  • 15
  • 21
  • Thanks for the correction on my code, i'm kind of new on zend atm! Still, i got the same doubts.. how to pass vars to my partial, and how to know the active tree.. that will help me a lot on my partial builder :\ – MGP Feb 13 '12 at 13:24
  • @MGP anytime you call a partial or partiaLoop to be rendered one of the things the constructor requires is a model (read array() or object()) of some kind. renderPartial() is no exception, the first argument is the model, in this case a Zend_Navigation_Container object. – RockyFord Feb 13 '12 at 13:30
  • and what about get the active tree, e.g., to add a particular class to all the links in that tree (or breadcrumb) to the last child? – MGP Feb 13 '12 at 14:20
  • @MGP you just went over my head, sorry – RockyFord Feb 13 '12 at 14:32
2

See HERE

Add this line to valid ACL if use ACL

if ($this->navigation()->accept($page))

Its result

...    
    foreach ( $iterator as $page ) {
        //VALID ACL
        if ($this->navigation()->accept($page)) {
            ...
            ...
        }
    }
    ..
Community
  • 1
  • 1
jd_7
  • 1,852
  • 13
  • 19