扩展 Zend View Helper 占位符

发布于 2024-08-27 02:20:51 字数 1107 浏览 14 评论 0原文

我正在阅读有关基本占位符用法的手册,它有这个例子:

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 添加更多的类值,如果可能的话,在渲染时,当所有内容都在占位符。我特别想做的一件事是将“first”类添加到第一个元素,将“last”类添加到最后一个元素。我假设我必须扩展 Zend_View_Helper_Placeholder 类才能完成此任务。

I was reading the manual about basic placeholder usage, and it has this example:

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

    // ...
}

I want to accomplish almost exactly that, but I'd like to conditionally add more class values to the repeating divs, at time of rendering if possible, when all the content is in the placeholder. One thing I specifically want to do is add the class of "first" to the first element and "last" to the last element. I assume that I'll have to extend the Zend_View_Helper_Placeholder class to accomplish this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2024-09-03 02:20:51

使用 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() ?>

The string set with setSeparator() is what will be used to implode elements in a container. Either set it to an empty string or just leave out the call to setSeparator() and insert the separating divs along with your other content:

  <?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() ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文