views:

374

answers:

1

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.

+1  A: 

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() ?>
Gordon
I actually want to set classes 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. (Updating question with these requirements as well)
Sonny
I ended up using your solution, so I'm awarding it as the answer. Thanks Gordon!
Sonny