1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect3 id="zend.view.helpers.initial.placeholder">
4 <title>Placeholder Helper</title>
7 The <classname>Placeholder</classname> view helper is used to persist content
8 between view scripts and view instances. It also offers some useful
9 features such as aggregating content, capturing view script content
10 for later use, and adding pre- and post-text to content (and custom
11 separators for aggregated content).
14 <example id="zend.view.helpers.initial.placeholder.usage">
15 <title>Basic Usage of Placeholders</title>
18 Basic usage of placeholders is to persist view data. Each invocation
19 of the <classname>Placeholder</classname> helper expects a placeholder name;
20 the helper then returns a placeholder container object that you can
21 either manipulate or simply echo out.
24 <programlisting language="php"><![CDATA[
25 <?php $this->placeholder('foo')->set("Some text for later") ?>
28 echo $this->placeholder('foo');
29 // outputs "Some text for later"
34 <example id="zend.view.helpers.initial.placeholder.aggregation">
35 <title>Using Placeholders to Aggregate Content</title>
38 Aggregating content via placeholders can be useful at times as well.
39 For instance, your view script may have a variable array from which
40 you wish to retrieve messages to display later; a later view script
41 can then determine how those will be rendered.
45 The <classname>Placeholder</classname> view helper uses containers that extend
46 <classname>ArrayObject</classname>, providing a rich featureset for
47 manipulating arrays. In addition, it offers a variety of methods for
48 formatting the content stored in the container:
54 <methodname>setPrefix($prefix)</methodname> sets text with which to
55 prefix the content. Use <methodname>getPrefix()</methodname> at any time
56 to determine what the current setting is.
62 <methodname>setPostfix($prefix)</methodname> sets text with which to
63 append the content. Use <methodname>getPostfix()</methodname> at any time
64 to determine what the current setting is.
70 <methodname>setSeparator($prefix)</methodname> sets text with which to
71 separate aggregated content. Use <methodname>getSeparator()</methodname>
72 at any time to determine what the current setting is.
78 <methodname>setIndent($prefix)</methodname> can be used to set an
79 indentation value for content. If an integer is passed,
80 that number of spaces will be used; if a string is passed,
81 the string will be used. Use <methodname>getIndent()</methodname>
82 at any time to determine what the current setting is.
87 <programlisting language="php"><![CDATA[
88 <!-- first view script -->
89 <?php $this->placeholder('foo')->exchangeArray($this->data) ?>
92 <programlisting language="php"><![CDATA[
93 <!-- later view script -->
95 $this->placeholder('foo')->setPrefix("<ul>\n <li>")
96 ->setSeparator("</li><li>\n")
98 ->setPostfix("</li></ul>\n");
102 echo $this->placeholder('foo');
103 // outputs as unordered list with pretty indentation
108 Because the <classname>Placeholder</classname> container objects extend
109 <classname>ArrayObject</classname>, you can also assign content to a specific
110 key in the container easily, instead of simply pushing it into the
111 container. Keys may be accessed either as object properties or as
115 <programlisting language="php"><![CDATA[
116 <?php $this->placeholder('foo')->bar = $this->data ?>
117 <?php echo $this->placeholder('foo')->bar ?>
120 $foo = $this->placeholder('foo');
126 <example id="zend.view.helpers.initial.placeholder.capture">
127 <title>Using Placeholders to Capture Content</title>
130 Occasionally you may have content for a placeholder in a view script
131 that is easiest to template; the <classname>Placeholder</classname> view
132 helper allows you to capture arbitrary content for later rendering
133 using the following <acronym>API</acronym>.
139 <methodname>captureStart($type, $key)</methodname> begins capturing
144 <varname>$type</varname> should be one of the
145 <classname>Placeholder</classname> constants <constant>APPEND</constant> or
146 <constant>SET</constant>. If <constant>APPEND</constant>, captured content
147 is appended to the list of current content in the
148 placeholder; if <constant>SET</constant>, captured content is used
149 as the sole value of the placeholder (potentially replacing
150 any previous content). By default, <varname>$type</varname> is
151 <constant>APPEND</constant>.
155 <varname>$key</varname> can be used to specify a specific key in
156 the placeholder container to which you want content
161 <methodname>captureStart()</methodname> locks capturing until
162 <methodname>captureEnd()</methodname> is called; you cannot nest
163 capturing with the same placeholder container. Doing so will
170 <methodname>captureEnd()</methodname> stops capturing content, and
171 places it in the container object according to how
172 <methodname>captureStart()</methodname> was called.
177 <programlisting language="php"><![CDATA[
178 <!-- Default capture: append -->
179 <?php $this->placeholder('foo')->captureStart();
180 foreach ($this->data as $datum): ?>
182 <h2><?php echo $datum->title ?></h2>
183 <p><?php echo $datum->content ?></p>
186 <?php $this->placeholder('foo')->captureEnd() ?>
188 <?php echo $this->placeholder('foo') ?>
191 <programlisting language="php"><![CDATA[
192 <!-- Capture to key -->
193 <?php $this->placeholder('foo')->captureStart('SET', 'data');
194 foreach ($this->data as $datum): ?>
196 <h2><?php echo $datum->title ?></h2>
197 <p><?php echo $datum->content ?></p>
200 <?php $this->placeholder('foo')->captureEnd() ?>
202 <?php echo $this->placeholder('foo')->data ?>
206 <sect4 id="zend.view.helpers.initial.placeholder.implementations">
207 <title>Concrete Placeholder Implementations</title>
210 Zend Framework ships with a number of "concrete" placeholder
211 implementations. These are for commonly used placeholders: doctype,
212 page title, and various <head> elements. In all cases, calling
213 the placeholder with no arguments returns the element itself.
217 Documentation for each element is covered separately, as linked
224 <link linkend="zend.view.helpers.initial.doctype">Doctype</link>
230 <link linkend="zend.view.helpers.initial.headlink">HeadLink</link>
236 <link linkend="zend.view.helpers.initial.headmeta">HeadMeta</link>
242 <link linkend="zend.view.helpers.initial.headscript">HeadScript</link>
248 <link linkend="zend.view.helpers.initial.headstyle">HeadStyle</link>
254 <link linkend="zend.view.helpers.initial.headtitle">HeadTitle</link>
260 <link linkend="zend.view.helpers.initial.inlinescript">InlineScript</link>