2009-08-07 111 views
8

我使用Zend_Navigation(甜除了框架,順便說一句)建立我的菜單後,它應該在的頁面(不言自明)渲染工作。我先在控制器中設置容器的地方:獲取Zend_Navigation菜單與jQuery的魚眼

// $pages is the array containing all page information 
$nav = new Zend_Navigation($pages); 
$this->view->navigation($nav); 

然後,在佈局,它呈現:

echo $this->navigation()->menu(); 

它完美的作品。現在:我希望菜單顯示有點不同。我正在構建的頁面使用jQuery Fisheye-plugin來構建一個類似Mac的Dock菜單。然而,這個插件需要一個特定的標記......

事實上,它需要包含<a>元素既是<img>(爲圖標)和<span>(工具提示)的列表。標準菜單視圖幫助器將所有內容呈現在無序列表中(邏輯上),其中'label'參數作爲鏈接文本。

這似乎傳遞給'label'參數內容呈現之前逃脫,所以插入HTML有不會做我任何好處。此外,魚眼通常看起來並不包含<li>標籤中包含的項目,整個項目包裝在<ul></ul>中,但僅包含<a>元素的一級列表。

我正在考慮爲碼頭寫一個自定義視圖助手,這將能夠照顧插入<img><span>,但我很難得到一個自定義視圖助手附加到導航類。即使我自己的其他自定義類(模型等)都由自動加載器親自處理,但我無法弄清楚它以何種方式放置它。對此有何想法?

然後,即使我可以讓這個視圖助手工作,我仍然留下了HTML無序列表 - 我知道我也可以使用自定義視圖助手失去那一個,但我一直是爲了語義,在列表中包含主導航菜單的粉絲。

如果有人能幫助我的話,我會非常感激。如果魚眼只是不打算與<ul>一起工作,那太糟糕了......在這種情況下是否會有完全失去Zend_Navigation的好理由?

回答

25

我還沒有看到的路要走無論是在製作自定義呈現的條款。下面是我落得這樣做雖然:

第一,而不是渲染默認菜單()調用,創建一個局部渲染成:

// menu.phtml is partial, cms is module 
$partial = array('menu.phtml', 'cms'); 
$this->navigation()->menu()->setPartial($partial); 
echo $this->navigation()->menu()->render(); 

然後(從文檔),您可以創建一個「定製」的渲染,像這樣:

// -- inside menu.phtml 
foreach ($this->container as $page) 
{ 
    // this just prints a "<a>" or "<span>" tag 
    // depending on whether the page has a uri 
    echo $this->menu()->htmlify($page), PHP_EOL; 
} 

我最終使我本來什麼(一個菜單,子菜單的一個級別),這個腳本:

// -- another menu.phtml 
<ul class="navigation"> 

<?php 

$html = array(); 

foreach ($this->container as $page) 
{ 
    $html[] = "<li>"; 
    $html[] = $this->menu()->htmlify($page) . PHP_EOL; 

    if (!empty($page->pages)) 
    { 
     $html[] = "<ul>"; 
     foreach ($page->pages as $subpage) 
     { 
      $html[] = "<li>"; 
      if ($href = $subpage->getHref()) $html[] = "<a href=\"{$href}\">"; 
      else $html[] = "<span>"; 
      $html[] = "<img src=\"/ui/cms/img/icons/edit.png\" alt=\"\"/>"; 
      $html[] = $subpage->getLabel(); 
      if ($href) $html[] = "</a>"; 
      else $html[] = "</span>";    
      $html[] = "</li>"; 
     } 
     $html[] = "</ul>"; 
    } 

    $html[] = "</li>"; 
} 

echo join(PHP_EOL, $html); 

?> 
</ul> 

您可以更改部分中的標記並在其中添加圖像/圖標。

+1

哇,完美的答案。似乎Zend_Navigation還有一些工作要做,但這會讓我得到我需要去的地方。謝謝! – JorenB 2009-08-13 21:47:58

+1

@JorenB就這樣你也知道了,我只是問Matthew P.(ZF的主要開發人員)是否會在任何時候包含用於導航的裝飾器。他說(總結)「沒有計劃,因爲部分有很少的開銷,裝飾者不被社區所喜愛......」看起來像Zend_Nav是一個相當的性能打擊。 – typeoneerror 2009-08-18 16:57:41

+0

我試過這個,但容器是空的...對象(Zend \ Navigation \ Navigation)#509(3){ [「pages」:protected] => array(0){ } [「index」:protected ] => array(0){ } [「dirtyIndex」:protected] => bool(false) } – cwhisperer 2013-10-16 17:52:30

7

對不起,在以前的帖子

的格式問題要添加自定義的助手請執行下列操作,在你的引導:

protected function _initNavigation() 
{ 
    $this->bootstrap('view');   // make sure we have a view 
    $view = $this->getResource('view');  // get the view resource 
    $config = new Zend_Config_Xml(APPLICATION_ROOT . '/config/navigation.xml','nav'); 
    $container = new Zend_Navigation($config); 
    $view->navigation($container); 

    // Tell Zend were it can find your custom helpers and what 
    // the prefix is going to be. 

    $view->addHelperPath(
      APPLICATION_ROOT . '/library/MyApp/View/Helper/Navigation', 
      'MyApp_View_Helper_' 
     ); 

} 

下一頁創建自定義視圖助手,並把它放在「 addHelperPath'路徑。根據你想要做什麼,你需要至少實現你自己的htmlify函數。以Zend/View/Help/Navigation/Menu.php爲例。

class MyApp_View_Helper_MyMenu extends Zend_View_Helper_Navigation_Menu 
{ 
    public function myMenu(Zend_Navigation_Container $container = null) 
    { 
     if (null !== $container) { 
      $this->setContainer($container); 
     } 
     return $this; 
    } 

    protected function htmlify(Zend_Navigation_Page $page) 
    { 
     ... 
    } 
} 

接下來,在您的PHTML腳本:

<?php echo $this->navigation()->myMenu()->setMaxDepth(0); ?> 

或只是

<?php echo $this->navigation()->myMenu(); ?> 

現在,如果你想自己的自定義屬性添加到生成的HTML,你可以將它們添加到您的導航ini或xml文件。例如:

<home> 
    <label>Home</label> 
    <uri>/</uri> 
    <img> 
     <src>/images/MyIcon.gif</src> 
    <img> 
</home> 

自定義XML標籤<img>將被存儲爲導航頁面的一個屬性,可以獲取的,如:

foreach ($iterator as $page) { 

    ... 

    $properties = new Zend_Config($page->GetCustomProperties()); 
    $myPicture = $properties->img->src; 

    ... 
} 

希望這有助於。 彼得

+0

您的解決方案幫助我解決了我的問題。謝謝! http://stackoverflow.com/questions/2364695/how-do-i-extend-the-zend-navigation-menu-view-helper – Sonny 2010-03-02 18:53:21