| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <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:
- -->
|