【问题标题】:Zend Navigation Custom RenderingZend Navigation 自定义渲染
【发布时间】:2012-02-13 12:17:17
【问题描述】:

我正在尝试为 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>';

提前谢谢大家!

【问题讨论】:

    标签: php zend-framework zend-navigation


    【解决方案1】:
    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;
    

    【讨论】:

    • 感谢您对我的代码的更正,我是 zend atm 的新手!尽管如此,我还是有同样的疑问..如何将变量传递给我的部分,以及如何知道活动树..这将对我的部分构建器有很大帮助:\
    • @MGP 每当您调用要呈现的部分或部分循环时,构造函数需要的东西之一是某种模型(读取数组()或对象())。 renderPartial() 也不例外,第一个参数是模型,在本例中是 Zend_Navigation_Container 对象。
    • 那么如何获取活动树,例如,将特定类添加到该树(或面包屑)中的所有链接到最后一个子节点?
    • @MGP 你刚刚超出我的想象,对不起
    【解决方案2】:

    HERE

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

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

    结果

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-10
      • 2013-07-27
      • 1970-01-01
      • 2017-06-09
      • 2012-08-25
      • 1970-01-01
      • 2017-11-24
      • 2013-09-25
      相关资源
      最近更新 更多