| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- Reviewed: no -->
- <sect3 id="zend.controller.actionhelpers.viewrenderer">
- <title>ViewRenderer</title>
- <sect4 id="zend.controller.actionhelper.viewrenderer.introduction">
- <title>Introduction</title>
- <para>
- The <code>ViewRenderer</code> helper is designed to satisfy the
- following goals:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- Eliminate the need to instantiate view objects within
- controllers; view objects will be automatically registered
- with the controller.
- </para>
- </listitem>
- <listitem>
- <para>
- Automatically set view script, helper, and filter paths
- based on the current module, and automatically associate
- the current module name as a class prefix for helper and
- filter classes.
- </para>
- </listitem>
- <listitem>
- <para>
- Create a globally available view object for all dispatched
- controllers and actions.
- </para>
- </listitem>
- <listitem>
- <para>
- Allow the developer to set default view rendering options
- for all controllers.
- </para>
- </listitem>
- <listitem>
- <para>
- Add the ability to automatically render a view script with
- no intervention.
- </para>
- </listitem>
- <listitem>
- <para>
- Allow the developer to create her own specifications for
- the view base path and for view script paths.
- </para>
- </listitem>
- </itemizedlist>
- <note>
- <para>
- If you perform a <code>_forward()</code>, redirect, or
- <code>render</code> manually, autorendering will not occur, as
- by performing any of these actions you are telling the
- <code>ViewRenderer</code> that you are determining your own
- output.
- </para>
- </note>
- <note>
- <para>
- The <code>ViewRenderer</code> is enabled by default. You may
- disable it via the front controller <code>noViewRenderer</code>
- param (<code>$front->setParam('noViewRenderer', true)</code>) or
- removing the helper from the helper broker stack
- (<classname>Zend_Controller_Action_HelperBroker::removeHelper('viewRenderer')</classname>).
- </para>
- <para>
- If you wish to modify settings of the <code>ViewRenderer</code>
- prior to dispatching the front controller, you may do so in one
- of two ways:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- Instantiate and register your own
- <code>ViewRenderer</code> object and pass it to the
- helper broker:
- </para>
- <programlisting language="php"><![CDATA[
- $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
- $viewRenderer->setView($view)
- ->setViewSuffix('php');
- Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
- ]]></programlisting>
- </listitem>
- <listitem>
- <para>
- Initialize and/or retrieve a <code>ViewRenderer</code>
- object on demand via the helper broker:
- </para>
- <programlisting language="php"><![CDATA[
- $viewRenderer =
- Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
- $viewRenderer->setView($view)
- ->setViewSuffix('php');
- ]]></programlisting>
- </listitem>
- </itemizedlist>
- </note>
- </sect4>
- <sect4 id="zend.controller.actionhelper.viewrenderer.api">
- <title>API</title>
- <para>
- At its most basic usage, you simply instantiate the
- <code>ViewRenderer</code> and pass it to the action helper broker.
- The easiest way to instantiate it and register in one go is to use
- the helper broker's <code>getStaticHelper()</code> method:
- </para>
- <programlisting language="php"><![CDATA[
- Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
- ]]></programlisting>
- <para>
- The first time an action controller is instantiated, it will trigger
- the <code>ViewRenderer</code> to instantiate a view object. Each
- time a controller is instantiated, the <code>ViewRenderer</code>'s
- <code>init()</code> method is called, which will cause it to set the
- view property of the action controller, and call
- <code>addScriptPath()</code> with a path relative to the current
- module; this will be called with a class prefix named after the
- current module, effectively namespacing all helper and filter
- classes you define for the module.
- </para>
- <para>
- Each time <code>postDispatch()</code> is called, it will call
- <code>render()</code> for the current action.
- </para>
- <para>
- As an example, consider the following class:
- </para>
- <programlisting language="php"><![CDATA[
- // A controller class, foo module:
- class Foo_BarController extends Zend_Controller_Action
- {
- // Render bar/index.phtml by default; no action required
- public function indexAction()
- {
- }
- // Render bar/populate.phtml with variable 'foo' set to 'bar'.
- // Since view object defined at preDispatch(), it's already available.
- public function populateAction()
- {
- $this->view->foo = 'bar';
- }
- }
- ...
- // in one of your view scripts:
- $this->foo(); // call Foo_View_Helper_Foo::foo()
- ]]></programlisting>
- <para>
- The <code>ViewRenderer</code> also defines a number of accessors to
- allow setting and retrieving view options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <code>setView($view)</code> allows you to set the view
- object for the <code>ViewRenderer</code>. It gets set as
- the public class property <varname>$view</varname>.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>setNeverRender($flag = true)</code> can be used to
- disable or enable autorendering globally, i.e., for all
- controllers. If set to true, <code>postDispatch()</code>
- will not automatically call <code>render()</code> in the
- current controller. <code>getNeverRender()</code> retrieves
- the current value.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>setNoRender($flag = true)</code> can be used to
- disable or enable autorendering. If set to true,
- <code>postDispatch()</code> will not automatically call
- <code>render()</code> in the current controller. This
- setting is reset each time <code>preDispatch()</code> is
- called (i.e., you need to set this flag for each controller
- for which you don't want autorenderering to occur).
- <code>getNoRender()</code> retrieves the current value.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>setNoController($flag = true)</code> can be used to
- tell <code>render()</code> not to look for the action script
- in a subdirectory named after the controller (which is the
- default behaviour). <code>getNoController()</code> retrieves
- the current value.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>setNeverController($flag = true)</code> is analogous
- to <code>setNoController()</code>, but works on a global
- level -- i.e., it will not be reset for each dispatched
- action. <code>getNeverController()</code> retrieves
- the current value.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>setScriptAction($name)</code> can be used to
- specify the action script to render. <varname>$name</varname>
- should be the name of the script minus the file suffix (and
- without the controller subdirectory, unless
- <code>noController</code> has been turned on). If not
- specified, it looks for a view script named after the action
- in the request object. <code>getScriptAction()</code>
- retrieves the current value.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>setResponseSegment($name)</code> can be used to
- specify which response object named segment to render into.
- If not specified, it renders into the default segment.
- <code>getResponseSegment()</code> retrieves the current
- value.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>initView($path, $prefix, $options)</code> may be called
- to specify the base view path, class prefix for helper and
- filter scripts, and <code>ViewRenderer</code> options. You
- may pass any of the following flags:
- <code>neverRender</code>, <code>noRender</code>,
- <code>noController</code>, <code>scriptAction</code>, and
- <code>responseSegment</code>.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>setRender($action = null, $name = null, $noController
- = false)</code> allows you to set any of
- <code>scriptAction</code>, <code>responseSegment</code>, and
- <code>noController</code> in one pass. <code>direct()</code>
- is an alias to this method, allowing you to call this method
- easily from your controller:
- </para>
- <programlisting language="php"><![CDATA[
- // Render 'foo' instead of current action script
- $this->_helper->viewRenderer('foo');
- // render form.phtml to the 'html' response segment, without using a
- // controller view script subdirectory:
- $this->_helper->viewRenderer('form', 'html', true);
- ]]></programlisting>
- <note><para>
- <code>setRender()</code> and <code>direct()</code> don't
- actually render the view script, but instead set hints
- that <code>postDispatch()</code> and
- <code>render()</code> will use to render the view.
- </para></note>
- </listitem>
- </itemizedlist>
- <para>
- The constructor allows you to optionally pass the view object and
- <code>ViewRenderer</code> options; it accepts the same flags as
- <code>initView()</code>:
- </para>
- <programlisting language="php"><![CDATA[
- $view = new Zend_View(array('encoding' => 'UTF-8'));
- $options = array('noController' => true, 'neverRender' => true);
- $viewRenderer =
- new Zend_Controller_Action_Helper_ViewRenderer($view, $options);
- ]]></programlisting>
- <para>
- There are several additional methods for customizing path
- specifications used for determining the view base path to add to the
- view object, and the view script path to use when autodetermining
- the view script to render. These methods each take one or more of
- the following placeholders:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <code>:moduleDir</code> refers to the current module's base
- directory (by convention, the parent directory of the
- module's controller directory).
- </para>
- </listitem>
- <listitem>
- <para>
- <code>:module</code> refers to the current module name.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>:controller</code> refers to the current controller name.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>:action</code> refers to the current action name.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>:suffix</code> refers to the view script suffix (which
- may be set via <code>setViewSuffix()</code>).
- </para>
- </listitem>
- </itemizedlist>
- <para>
- The methods for controlling path specifications are:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <code>setViewBasePathSpec($spec)</code> allows you to change
- the path specification used to determine the base path to
- add to the view object. The default specification is
- <code>:moduleDir/views</code>. You may retrieve the current
- specification at any time using
- <code>getViewBasePathSpec()</code>.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>setViewScriptPathSpec($spec)</code> allows you to
- change the path specification used to determine the path to
- an individual view script (minus the base view script path).
- The default specification is
- <code>:controller/:action.:suffix</code>. You may retrieve
- the current specification at any time using
- <code>getViewScriptPathSpec()</code>.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>setViewScriptPathNoControllerSpec($spec)</code> allows
- you to change the path specification used to determine the
- path to an individual view script when
- <code>noController</code> is in effect (minus the base view
- script path). The default specification is
- <code>:action.:suffix</code>. You may retrieve the current
- specification at any time using
- <code>getViewScriptPathNoControllerSpec()</code>.
- </para>
- </listitem>
- </itemizedlist>
- <para>
- For fine-grained control over path specifications, you may use
- <link linkend="zend.filter.inflector">Zend_Filter_Inflector</link>.
- Under the hood, the <code>ViewRenderer</code> uses an inflector to
- perform path mappings already. To interact with the inflector --
- either to set your own for use, or to modify the default inflector,
- the following methods may be used:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <code>getInflector()</code> will retrieve the inflector. If
- none exists yet in the <code>ViewRenderer</code>, it creates
- one using the default rules.
- </para>
- <para>
- By default, it uses static rule references for the suffix
- and module directory, as well as a static target; this
- allows various <code>ViewRenderer</code> properties the
- ability to dynamically modify the inflector.
- </para>
- </listitem>
- <listitem><para>
- <code>setInflector($inflector, $reference)</code> allows you
- to set a custom inflector for use with the
- <code>ViewRenderer</code>. If <varname>$reference</varname> is
- true, it will set the suffix and module directory as static
- references to <code>ViewRenderer</code> properties, as well
- as the target.
- </para></listitem>
- </itemizedlist>
- <note>
- <title>Default Lookup Conventions</title>
- <para>
- The <code>ViewRenderer</code> does some path normalization to
- make view script lookups easier. The default rules are as
- follows:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <code>:module</code>: MixedCase and camelCasedWords are separated by
- dashes, and the entire string cast to lowercase. E.g.:
- "FooBarBaz" becomes "foo-bar-baz".
- </para>
- <para>
- Internally, the inflector uses the filters
- <classname>Zend_Filter_Word_CamelCaseToDash</classname> and
- <classname>Zend_Filter_StringToLower</classname>.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>:controller</code>: MixedCase and camelCasedWords are separated
- by dashes; underscores are converted to directory
- separators, and the entire string cast to lower case.
- Examples: "FooBar" becomes "foo-bar"; "FooBar_Admin"
- becomes "foo-bar/admin".
- </para>
- <para>
- Internally, the inflector uses the filters
- <classname>Zend_Filter_Word_CamelCaseToDash</classname>,
- <classname>Zend_Filter_Word_UnderscoreToSeparator</classname>, and
- <classname>Zend_Filter_StringToLower</classname>.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>:action</code>: MixedCase and camelCasedWords are separated
- by dashes; non-alphanumeric characters are translated to
- dashes, and the entire string cast to lower case.
- Examples: "fooBar" becomes "foo-bar"; "foo-barBaz"
- becomes "foo-bar-baz".
- </para>
- <para>
- Internally, the inflector uses the filters
- <classname>Zend_Filter_Word_CamelCaseToDash</classname>,
- <classname>Zend_Filter_PregReplace</classname>, and
- <classname>Zend_Filter_StringToLower</classname>.
- </para>
- </listitem>
- </itemizedlist>
- </note>
- <para>
- The final items in the <code>ViewRenderer</code> API are the methods
- for actually determining view script paths and rendering views.
- These include:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <code>renderScript($script, $name)</code> allows you to
- render a script with a path you specify, optionally to a
- named path segment. When using this method, the
- <code>ViewRenderer</code> does no autodetermination of the
- script name, but instead directly passes the
- <varname>$script</varname> argument directly to the view object's
- <code>render()</code> method.
- </para>
- <note><para>
- Once the view has been rendered to the response object, it
- sets the <code>noRender</code> to prevent accidentally
- rendering the same view script multiple times.
- </para></note>
- <note>
- <para>
- By default,
- <classname>Zend_Controller_Action::renderScript()</classname>
- proxies to the <code>ViewRenderer</code>'s
- <code>renderScript()</code> method.
- </para>
- </note>
- </listitem>
- <listitem>
- <para>
- <code>getViewScript($action, $vars)</code> creates the path
- to a view script based on the action passed and/or any
- variables passed in <varname>$vars</varname>. Keys for this array
- may include any of the path specification keys ('moduleDir',
- 'module', 'controller', 'action', and 'suffix'). Any
- variables passed will be used; otherwise, values based on
- the current request will be utlized.
- </para>
- <para>
- <code>getViewScript()</code> will use either the
- <code>viewScriptPathSpec</code> or
- <code>viewScriptPathNoControllerSpec</code> based on the
- setting of the <code>noController</code> flag.
- </para>
- <para>
- Word delimiters occurring in module, controller, or action
- names will be replaced with dashes ('-'). Thus, if you have
- the controller name 'foo.bar' and the action 'baz:bat',
- using the default path specification will result in a view
- script path of 'foo-bar/baz-bat.phtml'.
- </para>
- <note>
- <para>
- By default,
- <classname>Zend_Controller_Action::getViewScript()</classname>
- proxies to the <code>ViewRenderer</code>'s
- <code>getViewScript()</code> method.
- </para>
- </note>
- </listitem>
- <listitem>
- <para>
- <code>render($action, $name, $noController)</code> checks
- first to see if either <varname>$name</varname> or
- <varname>$noController</varname> have been passed, and if so, sets
- the appropriate flags (responseSegment and noController,
- respectively) in the ViewRenderer. It then passes the
- <varname>$action</varname> argument, if any, on to
- <code>getViewScript()</code>. Finally, it passes the
- calculated view script path to <code>renderScript()</code>.
- </para>
- <note>
- <para>
- Be aware of the side-effects of using render(): the
- values you pass for the response segment name and for
- the noController flag will persist in the object.
- Additionally, noRender will be set after rendering is
- completed.
- </para>
- </note>
- <note>
- <para>
- By default,
- <classname>Zend_Controller_Action::render()</classname> proxies to
- the <code>ViewRenderer</code>'s <code>render()</code>
- method.
- </para>
- </note>
- </listitem>
- <listitem>
- <para>
- <code>renderBySpec($action, $vars, $name)</code> allows you
- to pass path specification variables in order to determine
- the view script path to create. It passes
- <varname>$action</varname> and <varname>$vars</varname> to
- <code>getScriptPath()</code>, and then passes the resulting
- script path and <varname>$name</varname> on to
- <code>renderScript()</code>.
- </para>
- </listitem>
- </itemizedlist>
- </sect4>
- <sect4 id="zend.controller.actionhelper.viewrenderer.basicusage">
- <title>Basic Usage Examples</title>
- <example id="zend.controller.actionhelper.viewrenderer.basicusage.example-1">
- <title>Basic Usage</title>
- <para>
- At its most basic, you simply initialize and register the
- <code>ViewRenderer</code> helper with the helper broker in your
- bootstrap, and then set variables in your action methods.
- </para>
- <programlisting language="php"><![CDATA[
- // In your bootstrap:
- Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
- ...
- // 'foo' module, 'bar' controller:
- class Foo_BarController extends Zend_Controller_Action
- {
- // Render bar/index.phtml by default; no action required
- public function indexAction()
- {
- }
- // Render bar/populate.phtml with variable 'foo' set to 'bar'.
- // Since view object defined at preDispatch(), it's already available.
- public function populateAction()
- {
- $this->view->foo = 'bar';
- }
- // Renders nothing as it forwards to another action; the new action
- // will perform any rendering
- public function bazAction()
- {
- $this->_forward('index');
- }
- // Renders nothing as it redirects to another location
- public function batAction()
- {
- $this->_redirect('/index');
- }
- }
- ]]></programlisting>
- </example>
- <note>
- <title>Naming Conventions: Word Delimiters in Controller and Action Names</title>
- <para>
- If your controller or action name is composed of several
- words, the dispatcher requires that these are separated on
- the URL by specific path and word delimiter characters. The
- <code>ViewRenderer</code> replaces any path delimiter found
- in the controller name with an actual path delimiter ('/'),
- and any word delimiter found with a dash ('-') when creating
- paths. Thus, a call to the action
- <code>/foo.bar/baz.bat</code> would dispatch to
- <code>FooBarController::bazBatAction()</code> in
- FooBarController.php, which would render
- <code>foo-bar/baz-bat.phtml</code>; a call to the action
- <code>/bar_baz/baz-bat</code> would dispatch to
- <code>Bar_BazController::bazBatAction()</code> in
- <code>Bar/BazController.php</code> (note the path
- separation) and render <code>bar/baz/baz-bat.phtml</code>.
- </para>
- <para>
- Note that the in the second example, the module is still the
- default module, but that, because of the existence of a path
- separator, the controller receives the name
- <code>Bar_BazController</code>, in
- <code>Bar/BazController.php</code>. The ViewRenderer mimics
- the controller directory hierarchy.
- </para>
- </note>
- <example id="zend.controller.actionhelper.viewrenderer.basicusage.example-2">
- <title>Disabling Autorender</title>
- <para>
- For some actions or controllers, you may want to turn off the
- autorendering -- for instance, if you're wanting to emit a
- different type of output (XML, JSON, etc), or if you simply want
- to emit nothing. You have two options: turn off all cases of
- autorendering (<code>setNeverRender()</code>), or simply turn it
- off for the current action (<code>setNoRender()</code>).
- </para>
- <programlisting language="php"><![CDATA[
- // Baz controller class, bar module:
- class Bar_BazController extends Zend_Controller_Action
- {
- public function fooAction()
- {
- // Don't auto render this action
- $this->_helper->viewRenderer->setNoRender();
- }
- }
- // Bat controller class, bar module:
- class Bar_BatController extends Zend_Controller_Action
- {
- public function preDispatch()
- {
- // Never auto render this controller's actions
- $this->_helper->viewRenderer->setNoRender();
- }
- }
- ]]></programlisting>
- </example>
- <note>
- <para>
- In most cases, it makes no sense to turn off autorendering
- globally (ala <code>setNeverRender()</code>), as the only thing
- you then gain from <code>ViewRenderer</code> is the autosetup of
- the view object.
- </para>
- </note>
- <example id="zend.controller.actionhelper.viewrenderer.basicusage.example-3">
- <title>Choosing a Different View Script</title>
- <para>
- Some situations require that you render a different script than
- one named after the action. For instance, if you have a
- controller that has both add and edit actions, they may both
- display the same 'form' view, albeit with different values set.
- You can easily change the script name used with either
- <code>setScriptAction()</code>, <code>setRender()</code>, or
- calling the helper as a method, which will invoke
- <code>setRender()</code>.
- </para>
- <programlisting language="php"><![CDATA[
- // Bar controller class, foo module:
- class Foo_BarController extends Zend_Controller_Action
- {
- public function addAction()
- {
- // Render 'bar/form.phtml' instead of 'bar/add.phtml'
- $this->_helper->viewRenderer('form');
- }
- public function editAction()
- {
- // Render 'bar/form.phtml' instead of 'bar/edit.phtml'
- $this->_helper->viewRenderer->setScriptAction('form');
- }
- public function processAction()
- {
- // do some validation...
- if (!$valid) {
- // Render 'bar/form.phtml' instead of 'bar/process.phtml'
- $this->_helper->viewRenderer->setRender('form');
- return;
- }
- // otherwise continue processing...
- }
- }
- ]]></programlisting>
- </example>
- <example id="zend.controller.actionhelper.viewrenderer.basicusage.example-4">
- <title>Modifying the Registered View</title>
- <para>
- What if you need to modify the view object -- for instance,
- change the helper paths, or the encoding? You can do so either
- by modifying the view object set in your controller, or by
- grabbing the view object out of the <code>ViewRenderer</code>;
- both are references to the same object.
- </para>
- <programlisting language="php"><![CDATA[
- // Bar controller class, foo module:
- class Foo_BarController extends Zend_Controller_Action
- {
- public function preDispatch()
- {
- // change view encoding
- $this->view->setEncoding('UTF-8');
- }
- public function bazAction()
- {
- // Get view object and set escape callback to 'htmlspecialchars'
- $view = $this->_helper->viewRenderer->view;
- $view->setEscape('htmlspecialchars');
- }
- }
- ]]></programlisting>
- </example>
- </sect4>
- <sect4 id="zend.controller.actionhelper.viewrenderer.advancedusage">
- <title>Advanced Usage Examples</title>
- <example id="zend.controller.actionhelper.viewrenderer.advancedusage.example-1">
- <title>Changing the Path Specifications</title>
- <para>
- In some circumstances, you may decide that the default path
- specifications do not fit your site's needs. For instance, you
- may want to have a single template tree to which you may then
- give access to your designers (this is very typical when using
- <ulink url="http://smarty.php.net/">Smarty</ulink>, for
- instance). In such a case, you may want to hardcode the view
- base path specification, and create an alternate specification
- for the action view script paths themselves.
- </para>
- <para>
- For purposes of this example, let's assume that the base path to
- views should be '/opt/vendor/templates', and that you wish for
- view scripts to be referenced by
- ':moduleDir/:controller/:action.:suffix'; if the noController
- flag has been set, you want to render out of the top level
- instead of in a subdirectory (':action.:suffix'). Finally, you
- want to use 'tpl' as the view script filename suffix.
- </para>
- <programlisting language="php"><![CDATA[
- /**
- * In your bootstrap:
- */
- // Different view implementation
- $view = new ZF_Smarty();
- $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
- $viewRenderer->setViewBasePathSpec('/opt/vendor/templates')
- ->setViewScriptPathSpec(':module/:controller/:action.:suffix')
- ->setViewScriptPathNoControllerSpec(':action.:suffix')
- ->setViewSuffix('tpl');
- Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
- ]]></programlisting>
- </example>
- <example id="zend.controller.actionhelper.viewrenderer.advancedusage.example-2">
- <title>Rendering Multiple View Scripts from a Single Action</title>
- <para>
- At times, you may need to render multiple view scripts from a
- single action. This is very straightforward -- simply make
- multiple calls to <code>render()</code>:
- </para>
- <programlisting language="php"><![CDATA[
- class SearchController extends Zend_Controller_Action
- {
- public function resultsAction()
- {
- // Assume $this->model is the current model
- $this->view->results =
- $this->model->find($this->_getParam('query', '');
- // render() by default proxies to the ViewRenderer
- // Render first the search form and then the results
- $this->render('form');
- $this->render('results');
- }
- public function formAction()
- {
- // do nothing; ViewRenderer autorenders the view script
- }
- }
- ]]></programlisting>
- </example>
- </sect4>
- </sect3>
- <!--
- vim:se ts=4 sw=4 et:
- -->
|