2012-02-13 119 views
3

我試圖創造zend的導航自定義導航,但我有兩個問題: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>'; 

預先感謝大家!

回答

4
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; 
+0

感謝對我的代碼進行更正,我對zend atm有點新了!儘管如此,我也遇到了同樣的疑問。如何將變量傳遞給我的部分,以及如何知道活動樹......這將對我的部分構建器有很大的幫助:\ – MGP 2012-02-13 13:24:55

+0

@MGP任何時候您稱部分或partiaLoop爲呈現構造函數需要的東西之一是某種模型(讀取數組()或對象())。 renderPartial()也不例外,第一個參數是模型,在這種情況下是一個Zend_Navigation_Container對象。 – RockyFord 2012-02-13 13:30:40

+0

以及如何獲得活動樹,例如,爲該樹(或麪包屑)中的所有鏈接添加特定類到最後一個孩子? – MGP 2012-02-13 14:20:55

2

HERE

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

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

其結果

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