Introduction

Basic Placeholder Usage

Zend Framework defines a generic placeholder() view helper that you may use for as many custom placeholders you need. It also provides a variety of specific placeholder implementations for often-needed functionality, such as specifying the DocType declaration, document title, and more.

All placeholders operate in roughly the same way. They are containers, and thus allow you to operate on them as collections. With them you can:

  • Append or prepend items to the collection.

  • Replace the entire collection with a single value.

  • Specify a string with which to prepend output of the collection when rendering.

  • Specify a string with which to append output of the collection when rendering.

  • Specify a string with which to separate items of the collection when rendering.

  • Capture content into the collection.

  • Render the aggregated content.

Typically, you will call the helper with no arguments, which will return a container on which you may operate. You will then either echo this container to render it, or call methods on it to configure or populate it. If the container is empty, rendering it will simply return an empty string; otherwise, the content will be aggregated according to the rules by which you configure it.

As an example, let's create a sidebar that consists of a number of "blocks" of content. You'll likely know up-front the structure of each block; let's assume for this example that it might look like this:

  1. <div class="sidebar">
  2.     <div class="block">
  3.         <p>
  4.             Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus
  5.             consectetur aliquet odio ac consectetur. Nulla quis eleifend
  6.             tortor. Pellentesque varius, odio quis bibendum consequat, diam
  7.             lectus porttitor quam, et aliquet mauris orci eu augue.
  8.         </p>
  9.     </div>
  10.     <div class="block">
  11.         <ul>
  12.             <li><a href="/some/target">Link</a></li>
  13.             <li><a href="/some/target">Link</a></li>
  14.         </ul>
  15.     </div>
  16. </div>

The content will vary based on the controller and action, but the structure will be the same. Let's first setup the sidebar in a resource method of our bootstrap:

  1. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  2. {
  3.     // ...
  4.  
  5.     protected function _initSidebar()
  6.     {
  7.         $this->bootstrap('View');
  8.         $view = $this->getResource('View');
  9.  
  10.         $view->placeholder('sidebar')
  11.              // "prefix" -> markup to emit once before all items in collection
  12.              ->setPrefix("<div class=\"sidebar\">\n    <div class=\"block\">\n")
  13.              // "separator" -> markup to emit between items in a collection
  14.              ->setSeparator("</div>\n    <div class=\"block\">\n")
  15.              // "postfix" -> markup to emit once after all items in a collection
  16.              ->setPostfix("</div>\n</div>");
  17.     }
  18.  
  19.     // ...
  20. }

The above defines a placeholder, "sidebar", that has no items. It configures the basic markup structure of that placeholder, however, per our requirements.

Now, let's assume for the "user" controller that for all actions we'll want a block at the top containing some information. We could accomplish this in two ways: (a) we could add the content to the placeholder directly in the controller's preDispatch() method, or (b) we could render a view script from within the preDispatch() method. We'll use (b), as it follows a more proper separation of concerns (leaving view-related logic and functionality within a view script).

We'll name the view script "user/_sidebar.phtml", and populate it as follows:

  1. <?php $this->placeholder('sidebar')->captureStart() ?>
  2. <h4>User Administration</h4>
  3. <ul>
  4.     <li><a href="<?php $this->url(array('action' => 'list')) ?>">
  5.         List</a></li>
  6.     <li><a href="<?php $this->url(array('action' => 'create')) ?>">
  7.         Create</a></a></li>
  8. </ul>
  9. <?php $this->placeholder('sidebar')->captureEnd() ?>

The above example makes use of the content capturing feature of placeholders. By default, content is appended as a new item in the container, allowing us to aggregate content. This example makes use of view helpers and static HTML in order to generate markup, and the content is then captured and appended into the placeholder itself.

To invoke the above view script, we would write the following in our preDispatch() method:

  1. class UserController extends Zend_Controller_Action
  2. {
  3.     // ...
  4.  
  5.     public function preDispatch()
  6.     {
  7.         // ...
  8.  
  9.         $this->view->render('user/_sidebar.phtml');
  10.  
  11.         // ...
  12.     }
  13.  
  14.     // ...
  15. }

Note that we're not capturing the rendered value; there's no need, as the entierty of that view is being captured into a placeholder.

Now, let's assume our "view" action in that same controller needs to present some information. Within the "user/view.phtml" view script, we might have the following snippet of content:

  1. $this->placeholder('sidebar')
  2.      ->append('<p>User: ' . $this->escape($this->username)'</p>');

This example makes use of the append() method, and passes it some simple markup to aggregate.

Finally, let's modify our layout view script, and have it render the placeholder.

  1. <html>
  2. <head>
  3.     <title>My Site</title>
  4. </head>
  5. <body>
  6.     <div class="content">
  7.         <?php echo $this->layout()->content ?>
  8.     </div>
  9.     <?php echo $this->placeholder('sidebar') ?>
  10. </body>
  11. </html>

For controllers and actions that do not populate the "sidebar" placeholder, no content will be rendered; for those that do, however, echoing the placeholder will render the content according to the rules we created in our bootstrap, and the content we aggregated throughout the application. In the case of the "/user/view" action, and assuming a username of "matthew", we would get content for the sidebar as follows (formatted for readability):

  1. <div class="sidebar">
  2.     <div class="block">
  3.         <h4>User Administration</h4>
  4.         <ul>
  5.             <li><a href="/user/list">List</a></li>
  6.             <li><a href="/user/create">Create</a></a></li>
  7.         </ul>
  8.     </div>
  9.     <div class="block">
  10.         <p>User: matthew</p>
  11.     </div>
  12. </div>

There are a large number of things you can do by combining placeholders and layout scripts; experiment with them, and read the relevant manual sections for more information.


Introduction