2010-03-25 88 views
2

我閱讀手冊約basic placeholder usage,它有這個例子:擴展的Zend視圖助手佔位

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{ 
    // ... 

    protected function _initSidebar() 
    { 
     $this->bootstrap('View'); 
     $view = $this->getResource('View'); 

     $view->placeholder('sidebar') 
      // "prefix" -> markup to emit once before all items in collection 
      ->setPrefix("<div class=\"sidebar\">\n <div class=\"block\">\n") 
      // "separator" -> markup to emit between items in a collection 
      ->setSeparator("</div>\n <div class=\"block\">\n") 
      // "postfix" -> markup to emit once after all items in a collection 
      ->setPostfix("</div>\n</div>"); 
    } 

    // ... 
} 

我要完成的幾乎一模一樣,但我想有條件地添加更多的類值的如果可能的話,在所有內容都在佔位符中時重複div s。我特別想做的一件事是將「first」類添加到第一個元素,將「last」添加到最後一個元素。我假設我將不得不延長Zend_View_Helper_Placeholder課程來完成此任務。

回答

1

setSeparator()設置的字符串是用來爆炸容器中元素的內容。無論是將其設置爲空字符串或者只是離開了呼叫setSeparator(),並連同其他內容插入分離的div:

<?php $this->placeholder('sidebar')->captureStart(); ?> 

    <?php if($userIsAdmin === TRUE) { ?> 

     <div class="block admin-menu"> 
     <h4>User Administration</h4> 
     <ul> 
      <li> ... </li> 
      <li> ... </li> 
     </ul> 
     </div> 

    <?php } ?> 

     <div class="block other-stuff">  
      <h4>Non-Admin Stuff</h4> 
      <ul> 
       <li> ... </li> 
       <li> ... </li> 
      </ul> 
     </div> 

    <?php $this->placeholder('sidebar')->captureEnd() ?> 
+0

其實我是想在渲染如果可能的話,當所有的內容的時候設置班在佔位符中。我特別想做的一件事是將「first」類添加到第一個元素,將「last」添加到最後一個元素。 (根據這些要求更新問題) – Sonny 2010-04-05 15:29:57

+0

我最終使用了您的解決方案,因此我將其作爲答案授予它。謝謝戈登! – Sonny 2010-04-06 20:59:13