Zend导航自定义渲染


Zend Navigation Custom Rendering

我试图为zend导航创建一个自定义导航,但我有两个问题:

  1. 如何将变量传递到自定义的部分phtml,或者如果可能的话
  2. 如何通过整个活动菜单树设置类

这是我迄今为止的代码:

在控制器中:

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

在视图中:

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

和我的部分:

<?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>';

提前感谢大家!

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

不是很正确,你有正确的想法,但试试

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

这是api:

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

这一点看起来也有点不稳定:

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

我不认为->addPages()是你想要的,我认为你需要的是:

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

请参阅此处

如果使用ACL ,请将此行添加到有效ACL

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

其结果

...    
    foreach ( $iterator as $page ) {
        //VALID ACL
        if ($this->navigation()->accept($page)) {
            ...
            ...
        }
    }
    ..