1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="learning.view.placeholders.basics">
4 <title>Basic Placeholder Usage</title>
7 Zend Framework defines a generic <methodname>placeholder()</methodname> view helper that you
8 may use for as many custom placeholders you need. It also provides a variety of specific
9 placeholder implementations for often-needed functionality, such as specifying the
10 <emphasis>DocType</emphasis> declaration, document title, and more.
14 All placeholders operate in roughly the same way. They are containers, and thus allow you to
15 operate on them as collections. With them you can:
21 <emphasis>Append</emphasis> or <emphasis>prepend</emphasis> items to the collection.
27 <emphasis>Replace</emphasis> the entire collection with a single value.
33 Specify a string with which to <emphasis>prepend output</emphasis> of the collection
40 Specify a string with which to <emphasis>append output</emphasis> of the collection
47 Specify a string with which to <emphasis>separate items</emphasis> of the collection
54 <emphasis>Capture content</emphasis> into the collection.
60 <emphasis>Render</emphasis> the aggregated content.
66 Typically, you will call the helper with no arguments, which will return a container on
67 which you may operate. You will then either echo this container to render it, or call
68 methods on it to configure or populate it. If the container is empty, rendering it will
69 simply return an empty string; otherwise, the content will be aggregated according to the
70 rules by which you configure it.
74 As an example, let's create a sidebar that consists of a number of "blocks" of content.
75 You'll likely know up-front the structure of each block; let's assume for this example that
76 it might look like this:
79 <programlisting language="html"><![CDATA[
83 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus
84 consectetur aliquet odio ac consectetur. Nulla quis eleifend
85 tortor. Pellentesque varius, odio quis bibendum consequat, diam
86 lectus porttitor quam, et aliquet mauris orci eu augue.
91 <li><a href="/some/target">Link</a></li>
92 <li><a href="/some/target">Link</a></li>
99 The content will vary based on the controller and action, but the structure will be the
100 same. Let's first setup the sidebar in a resource method of our bootstrap:
103 <programlisting language="php"><![CDATA[
104 class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
108 protected function _initSidebar()
110 $this->bootstrap('View');
111 $view = $this->getResource('View');
113 $view->placeholder('sidebar')
114 // "prefix" -> markup to emit once before all items in collection
115 ->setPrefix("<div class=\"sidebar\">\n <div class=\"block\">\n")
116 // "separator" -> markup to emit between items in a collection
117 ->setSeparator("</div>\n <div class=\"block\">\n")
118 // "postfix" -> markup to emit once after all items in a collection
119 ->setPostfix("</div>\n</div>");
127 The above defines a placeholder, "sidebar", that has no items. It configures the basic
128 markup structure of that placeholder, however, per our requirements.
132 Now, let's assume for the "user" controller that for all actions we'll want a block at the
133 top containing some information. We could accomplish this in two ways: (a) we could add the
134 content to the placeholder directly in the controller's
135 <methodname>preDispatch()</methodname> method, or (b) we could render a view script from
136 within the <methodname>preDispatch()</methodname> method. We'll use (b), as it follows a
137 more proper separation of concerns (leaving view-related logic and functionality within a
142 We'll name the view script "<filename>user/_sidebar.phtml</filename>", and populate it as
146 <programlisting language="php"><![CDATA[
147 <?php $this->placeholder('sidebar')->captureStart() ?>
148 <h4>User Administration</h4>
150 <li><a href="<?php $this->url(array('action' => 'list')) ?>">
152 <li><a href="<?php $this->url(array('action' => 'create')) ?>">
155 <?php $this->placeholder('sidebar')->captureEnd() ?>
159 The above example makes use of the content capturing feature of placeholders. By default,
160 content is appended as a new item in the container, allowing us to aggregate content. This
161 example makes use of view helpers and static <acronym>HTML</acronym> in order to generate
162 markup, and the content is then captured and appended into the placeholder itself.
166 To invoke the above view script, we would write the following in our
167 <methodname>preDispatch()</methodname> method:
170 <programlisting language="php"><![CDATA[
171 class UserController extends Zend_Controller_Action
175 public function preDispatch()
179 $this->view->render('user/_sidebar.phtml');
189 Note that we're not capturing the rendered value; there's no need, as the entierty of that
190 view is being captured into a placeholder.
194 Now, let's assume our "view" action in that same controller needs to present some
195 information. Within the "<filename>user/view.phtml</filename>" view script, we might have
196 the following snippet of content:
199 <programlisting language="php"><![CDATA[
200 $this->placeholder('sidebar')
201 ->append('<p>User: ' . $this->escape($this->username) . '</p>');
205 This example makes use of the <methodname>append()</methodname> method, and passes it some
206 simple markup to aggregate.
210 Finally, let's modify our layout view script, and have it render the placeholder.
213 <programlisting language="php"><![CDATA[
216 <title>My Site</title>
219 <div class="content">
220 <?php echo $this->layout()->content ?>
222 <?php echo $this->placeholder('sidebar') ?>
228 For controllers and actions that do not populate the "sidebar" placeholder, no content will
229 be rendered; for those that do, however, echoing the placeholder will render the content
230 according to the rules we created in our bootstrap, and the content we aggregated throughout
231 the application. In the case of the "<filename>/user/view</filename>" action, and assuming a
232 username of "matthew", we would get content for the sidebar as follows (formatted for
236 <programlisting language="html"><![CDATA[
237 <div class="sidebar">
239 <h4>User Administration</h4>
241 <li><a href="/user/list">List</a></li>
242 <li><a href="/user/create">Create</a></a></li>
252 There are a large number of things you can do by combining placeholders and layout scripts;
253 experiment with them, and read the <link
254 linkend="zend.view.helpers.initial.placeholder">relevant manual sections</link> for more