1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
<sect1 id="zend.view.introduction">
<title>Pendahuluan</title>
<para>
Zend_View adalah class yang dapat Anda gunakan untuk bekerja dengan
bagian "view" dari pola model-view-controller. Yaitu untuk menjaga
view script terpisah dari model dan controller script. Zend_View
menyediakan sebuah sistem pembantu (helpers), output filters dan
variable escaping.
</para>
<para>
Zend_View is template system agnostic; you may use PHP as
your template language, or create instances of other
template systems and manipulate them within your view
script.
</para>
<para>
Essentially, using Zend_View happens in two major steps:
1. Your controller script creates an instance of
Zend_View and assigns variables to that instance.
2. The controller tells the Zend_View to render a particular
view, thereby handing control over the view script, which
generates the view output.
</para>
<sect2 id="zend.view.introduction.controller">
<title>Controller Script</title>
<para>
As a simple example, let us say your controller has a list
of book data that it wants to have rendered by a view. The
controller script might look something like this:
</para>
<programlisting role="php"><![CDATA[<?php
// use a model to get the data for book authors and titles.
$data = array(
array(
'author' => 'Hernando de Soto',
'title' => 'The Mystery of Capitalism'
),
array(
'author' => 'Henry Hazlitt',
'title' => 'Economics in One Lesson'
),
array(
'author' => 'Milton Friedman',
'title' => 'Free to Choose'
)
);
// now assign the book data to a Zend_View instance
Zend_Loader::loadClass('Zend_View');
$view = new Zend_View();
$view->books = $data;
// and render a view script called "booklist.php"
echo $view->render('booklist.php');]]>
</programlisting>
</sect2>
<sect2 id="zend.view.introduction.view">
<title>View Script</title>
<para>
Now we need the associated view script, "booklist.php".
This is a PHP script like any other, with one exception: it
executes inside the scope of the Zend_View instance, which
means that references to $this point to the Zend_View
instance properties and methods. (Variables assigned to the
instance by the controller are public properties of the
Zend_View instance.) Thus, a very basic view script could
look like this:
</para>
<programlisting role="php"><![CDATA[<?php if ($this->books): ?>
<!-- A table of some books. -->
<table>
<tr>
<th>Author</th>
<th>Title</th>
</tr>
<?php foreach ($this->books as $key => $val): ?>
<tr>
<td><?php echo $this->escape($val['author']) ?></td>
<td><?php echo $this->escape($val['title']) ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php else: ?>
<p>There are no books to display.</p>
<?php endif;]]>
</programlisting>
<para>
Note how we use the "escape()" method to apply output
escaping to variables.
</para>
</sect2>
<sect2 id="zend.view.introduction.options">
<title>Options</title>
<para>
<code>Zend_View</code> has several options that may be set to
configure the behaviour of your view scripts.
</para>
<itemizedlist>
<listitem>
<para>
<code>basePath:</code> indicate a base path from which to set
the script, helper, and filter path. It assumes a directory
structure of:
</para>
<programlisting role="php"><![CDATA[
base/path/
helpers/
filters/
scripts/]]>
</programlisting>
<para>
This may be set via <code>setBasePath()</code>,
<code>addBasePath()</code>, or the <code>basePath</code>
option to the constructor.
</para>
</listitem>
<listitem><para>
<code>encoding:</code> indicate the character encoding to use
with <code>htmlentities()</code>,
<code>htmlspecialchars()</code>, and other operations. Defaults
to ISO-8859-1 (latin1). May be set via
<code>setEncoding()</code> or the <code>encoding</code> option
to the constructor.
</para></listitem>
<listitem><para>
<code>escape:</code> indicate a callback to be used by
<code>escape()</code>. May be set via <code>setEscape()</code>
or the <code>escape</code> option to the constructor.
</para></listitem>
<listitem><para>
<code>filter:</code> indicate a filter to use after rendering
a view script. May be set via <code>setFilter()</code>,
<code>addFilter()</code>, or the <code>filter</code> option to
the constructor.
</para></listitem>
<listitem><para>
<code>strictVars:</code> force <code>Zend_View</code> to emit
notices and warnings when uninitialized view variables are
accessed. This may be set by calling
<code>strictVars(true)</code> or passing the
<code>strictVars</code> option to the constructor.
</para></listitem>
</itemizedlist>
</sect2>
<sect2 id="zend.view.introduction.accessors">
<title>Utility Accessors</title>
<para>
Typically, you'll only ever need to call on <code>assign()</code>,
<code>render()</code>, or one of the methods for setting/adding
filter, helper, and script paths. However, if you wish to extend
<code>Zend_View</code> yourself, or need access to some of its
internals, a number of accessors exist:
</para>
<itemizedlist>
<listitem>
<para>
<code>getVars()</code> will return all assigned variables.
</para>
</listitem>
<listitem>
<para>
<code>clearVars()</code> will clear all assigned variables;
useful when you wish to re-use a view object, but want to
control what variables are available..
</para>
</listitem>
<listitem>
<para>
<code>getScriptPath($script)</code> will retrieve the
resolved path to a given view script.
</para>
</listitem>
<listitem>
<para>
<code>getScriptPaths()</code> will retrieve all registered
script paths.
</para>
</listitem>
<listitem>
<para>
<code>getHelperPath($helper)</code> will retrieve the
resolved path to the named helper class.
</para>
</listitem>
<listitem>
<para>
<code>getHelperPaths()</code> will retrieve all registered
helper paths.
</para>
</listitem>
<listitem>
<para>
<code>getFilterPath($filter)</code> will retrieve the
resolved path to the named filter class.
</para>
</listitem>
<listitem>
<para>
<code>getFilterPaths()</code> will retrieve all registered
filter paths.
</para>
</listitem>
</itemizedlist>
</sect2>
</sect1>
<!--
vim:se ts=4 sw=4 et:
-->
|