【问题标题】:Extending Zend View Helper Placeholder扩展 Zend View Helper 占位符
【发布时间】:2011-01-31 18:56:55
【问题描述】:

我正在阅读有关basic placeholder usage 的手册,其中有以下示例:

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>");
    }

    // ...
}

我想几乎完全做到这一点,但我想有条件地向重复的divs 添加更多类值,如果可能的话,在渲染时,当所有内容都在占位符中时。我特别想做的一件事是将“first”类添加到第一个元素,将“last”类添加到最后一个元素。我假设我必须扩展 Zend_View_Helper_Placeholder 类来完成此操作。

【问题讨论】:

    标签: php zend-framework placeholder zend-view


    【解决方案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() ?>
    

    【讨论】:

    • 如果可能,当所有内容都在占位符中时,我实际上想在渲染时设置类。我特别想做的一件事是将“first”类添加到第一个元素,将“last”类添加到最后一个元素。 (也根据这些要求更新问题)
    • 我最终使用了您的解决方案,因此我将其授予答案。谢谢戈登!
    猜你喜欢
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多