Zend_Controller-ActionHelpers-ViewRenderer.xml 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect3 id="zend.controller.actionhelpers.viewrenderer">
  4. <title>ViewRenderer</title>
  5. <sect4 id="zend.controller.actionhelper.viewrenderer.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. The <code>ViewRenderer</code> helper is designed to satisfy the
  9. following goals:
  10. </para>
  11. <itemizedlist>
  12. <listitem>
  13. <para>
  14. Eliminate the need to instantiate view objects within
  15. controllers; view objects will be automatically registered
  16. with the controller.
  17. </para>
  18. </listitem>
  19. <listitem>
  20. <para>
  21. Automatically set view script, helper, and filter paths
  22. based on the current module, and automatically associate
  23. the current module name as a class prefix for helper and
  24. filter classes.
  25. </para>
  26. </listitem>
  27. <listitem>
  28. <para>
  29. Create a globally available view object for all dispatched
  30. controllers and actions.
  31. </para>
  32. </listitem>
  33. <listitem>
  34. <para>
  35. Allow the developer to set default view rendering options
  36. for all controllers.
  37. </para>
  38. </listitem>
  39. <listitem>
  40. <para>
  41. Add the ability to automatically render a view script with
  42. no intervention.
  43. </para>
  44. </listitem>
  45. <listitem>
  46. <para>
  47. Allow the developer to create her own specifications for
  48. the view base path and for view script paths.
  49. </para>
  50. </listitem>
  51. </itemizedlist>
  52. <note>
  53. <para>
  54. If you perform a <code>_forward()</code>, redirect, or
  55. <code>render</code> manually, autorendering will not occur, as
  56. by performing any of these actions you are telling the
  57. <code>ViewRenderer</code> that you are determining your own
  58. output.
  59. </para>
  60. </note>
  61. <note>
  62. <para>
  63. The <code>ViewRenderer</code> is enabled by default. You may
  64. disable it via the front controller <code>noViewRenderer</code>
  65. param (<code>$front->setParam('noViewRenderer', true)</code>) or
  66. removing the helper from the helper broker stack
  67. (<classname>Zend_Controller_Action_HelperBroker::removeHelper('viewRenderer')</classname>).
  68. </para>
  69. <para>
  70. If you wish to modify settings of the <code>ViewRenderer</code>
  71. prior to dispatching the front controller, you may do so in one
  72. of two ways:
  73. </para>
  74. <itemizedlist>
  75. <listitem>
  76. <para>
  77. Instantiate and register your own
  78. <code>ViewRenderer</code> object and pass it to the
  79. helper broker:
  80. </para>
  81. <programlisting language="php"><![CDATA[
  82. $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
  83. $viewRenderer->setView($view)
  84. ->setViewSuffix('php');
  85. Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
  86. ]]></programlisting>
  87. </listitem>
  88. <listitem>
  89. <para>
  90. Initialize and/or retrieve a <code>ViewRenderer</code>
  91. object on demand via the helper broker:
  92. </para>
  93. <programlisting language="php"><![CDATA[
  94. $viewRenderer =
  95. Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  96. $viewRenderer->setView($view)
  97. ->setViewSuffix('php');
  98. ]]></programlisting>
  99. </listitem>
  100. </itemizedlist>
  101. </note>
  102. </sect4>
  103. <sect4 id="zend.controller.actionhelper.viewrenderer.api">
  104. <title>API</title>
  105. <para>
  106. At its most basic usage, you simply instantiate the
  107. <code>ViewRenderer</code> and pass it to the action helper broker.
  108. The easiest way to instantiate it and register in one go is to use
  109. the helper broker's <code>getStaticHelper()</code> method:
  110. </para>
  111. <programlisting language="php"><![CDATA[
  112. Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  113. ]]></programlisting>
  114. <para>
  115. The first time an action controller is instantiated, it will trigger
  116. the <code>ViewRenderer</code> to instantiate a view object. Each
  117. time a controller is instantiated, the <code>ViewRenderer</code>'s
  118. <code>init()</code> method is called, which will cause it to set the
  119. view property of the action controller, and call
  120. <code>addScriptPath()</code> with a path relative to the current
  121. module; this will be called with a class prefix named after the
  122. current module, effectively namespacing all helper and filter
  123. classes you define for the module.
  124. </para>
  125. <para>
  126. Each time <code>postDispatch()</code> is called, it will call
  127. <code>render()</code> for the current action.
  128. </para>
  129. <para>
  130. As an example, consider the following class:
  131. </para>
  132. <programlisting language="php"><![CDATA[
  133. // A controller class, foo module:
  134. class Foo_BarController extends Zend_Controller_Action
  135. {
  136. // Render bar/index.phtml by default; no action required
  137. public function indexAction()
  138. {
  139. }
  140. // Render bar/populate.phtml with variable 'foo' set to 'bar'.
  141. // Since view object defined at preDispatch(), it's already available.
  142. public function populateAction()
  143. {
  144. $this->view->foo = 'bar';
  145. }
  146. }
  147. ...
  148. // in one of your view scripts:
  149. $this->foo(); // call Foo_View_Helper_Foo::foo()
  150. ]]></programlisting>
  151. <para>
  152. The <code>ViewRenderer</code> also defines a number of accessors to
  153. allow setting and retrieving view options:
  154. </para>
  155. <itemizedlist>
  156. <listitem>
  157. <para>
  158. <code>setView($view)</code> allows you to set the view
  159. object for the <code>ViewRenderer</code>. It gets set as
  160. the public class property <varname>$view</varname>.
  161. </para>
  162. </listitem>
  163. <listitem>
  164. <para>
  165. <code>setNeverRender($flag = true)</code> can be used to
  166. disable or enable autorendering globally, i.e., for all
  167. controllers. If set to true, <code>postDispatch()</code>
  168. will not automatically call <code>render()</code> in the
  169. current controller. <code>getNeverRender()</code> retrieves
  170. the current value.
  171. </para>
  172. </listitem>
  173. <listitem>
  174. <para>
  175. <code>setNoRender($flag = true)</code> can be used to
  176. disable or enable autorendering. If set to true,
  177. <code>postDispatch()</code> will not automatically call
  178. <code>render()</code> in the current controller. This
  179. setting is reset each time <code>preDispatch()</code> is
  180. called (i.e., you need to set this flag for each controller
  181. for which you don't want autorenderering to occur).
  182. <code>getNoRender()</code> retrieves the current value.
  183. </para>
  184. </listitem>
  185. <listitem>
  186. <para>
  187. <code>setNoController($flag = true)</code> can be used to
  188. tell <code>render()</code> not to look for the action script
  189. in a subdirectory named after the controller (which is the
  190. default behaviour). <code>getNoController()</code> retrieves
  191. the current value.
  192. </para>
  193. </listitem>
  194. <listitem>
  195. <para>
  196. <code>setNeverController($flag = true)</code> is analogous
  197. to <code>setNoController()</code>, but works on a global
  198. level -- i.e., it will not be reset for each dispatched
  199. action. <code>getNeverController()</code> retrieves
  200. the current value.
  201. </para>
  202. </listitem>
  203. <listitem>
  204. <para>
  205. <code>setScriptAction($name)</code> can be used to
  206. specify the action script to render. <varname>$name</varname>
  207. should be the name of the script minus the file suffix (and
  208. without the controller subdirectory, unless
  209. <code>noController</code> has been turned on). If not
  210. specified, it looks for a view script named after the action
  211. in the request object. <code>getScriptAction()</code>
  212. retrieves the current value.
  213. </para>
  214. </listitem>
  215. <listitem>
  216. <para>
  217. <code>setResponseSegment($name)</code> can be used to
  218. specify which response object named segment to render into.
  219. If not specified, it renders into the default segment.
  220. <code>getResponseSegment()</code> retrieves the current
  221. value.
  222. </para>
  223. </listitem>
  224. <listitem>
  225. <para>
  226. <code>initView($path, $prefix, $options)</code> may be called
  227. to specify the base view path, class prefix for helper and
  228. filter scripts, and <code>ViewRenderer</code> options. You
  229. may pass any of the following flags:
  230. <code>neverRender</code>, <code>noRender</code>,
  231. <code>noController</code>, <code>scriptAction</code>, and
  232. <code>responseSegment</code>.
  233. </para>
  234. </listitem>
  235. <listitem>
  236. <para>
  237. <code>setRender($action = null, $name = null, $noController
  238. = false)</code> allows you to set any of
  239. <code>scriptAction</code>, <code>responseSegment</code>, and
  240. <code>noController</code> in one pass. <code>direct()</code>
  241. is an alias to this method, allowing you to call this method
  242. easily from your controller:
  243. </para>
  244. <programlisting language="php"><![CDATA[
  245. // Render 'foo' instead of current action script
  246. $this->_helper->viewRenderer('foo');
  247. // render form.phtml to the 'html' response segment, without using a
  248. // controller view script subdirectory:
  249. $this->_helper->viewRenderer('form', 'html', true);
  250. ]]></programlisting>
  251. <note><para>
  252. <code>setRender()</code> and <code>direct()</code> don't
  253. actually render the view script, but instead set hints
  254. that <code>postDispatch()</code> and
  255. <code>render()</code> will use to render the view.
  256. </para></note>
  257. </listitem>
  258. </itemizedlist>
  259. <para>
  260. The constructor allows you to optionally pass the view object and
  261. <code>ViewRenderer</code> options; it accepts the same flags as
  262. <code>initView()</code>:
  263. </para>
  264. <programlisting language="php"><![CDATA[
  265. $view = new Zend_View(array('encoding' => 'UTF-8'));
  266. $options = array('noController' => true, 'neverRender' => true);
  267. $viewRenderer =
  268. new Zend_Controller_Action_Helper_ViewRenderer($view, $options);
  269. ]]></programlisting>
  270. <para>
  271. There are several additional methods for customizing path
  272. specifications used for determining the view base path to add to the
  273. view object, and the view script path to use when autodetermining
  274. the view script to render. These methods each take one or more of
  275. the following placeholders:
  276. </para>
  277. <itemizedlist>
  278. <listitem>
  279. <para>
  280. <code>:moduleDir</code> refers to the current module's base
  281. directory (by convention, the parent directory of the
  282. module's controller directory).
  283. </para>
  284. </listitem>
  285. <listitem>
  286. <para>
  287. <code>:module</code> refers to the current module name.
  288. </para>
  289. </listitem>
  290. <listitem>
  291. <para>
  292. <code>:controller</code> refers to the current controller name.
  293. </para>
  294. </listitem>
  295. <listitem>
  296. <para>
  297. <code>:action</code> refers to the current action name.
  298. </para>
  299. </listitem>
  300. <listitem>
  301. <para>
  302. <code>:suffix</code> refers to the view script suffix (which
  303. may be set via <code>setViewSuffix()</code>).
  304. </para>
  305. </listitem>
  306. </itemizedlist>
  307. <para>
  308. The methods for controlling path specifications are:
  309. </para>
  310. <itemizedlist>
  311. <listitem>
  312. <para>
  313. <code>setViewBasePathSpec($spec)</code> allows you to change
  314. the path specification used to determine the base path to
  315. add to the view object. The default specification is
  316. <code>:moduleDir/views</code>. You may retrieve the current
  317. specification at any time using
  318. <code>getViewBasePathSpec()</code>.
  319. </para>
  320. </listitem>
  321. <listitem>
  322. <para>
  323. <code>setViewScriptPathSpec($spec)</code> allows you to
  324. change the path specification used to determine the path to
  325. an individual view script (minus the base view script path).
  326. The default specification is
  327. <code>:controller/:action.:suffix</code>. You may retrieve
  328. the current specification at any time using
  329. <code>getViewScriptPathSpec()</code>.
  330. </para>
  331. </listitem>
  332. <listitem>
  333. <para>
  334. <code>setViewScriptPathNoControllerSpec($spec)</code> allows
  335. you to change the path specification used to determine the
  336. path to an individual view script when
  337. <code>noController</code> is in effect (minus the base view
  338. script path). The default specification is
  339. <code>:action.:suffix</code>. You may retrieve the current
  340. specification at any time using
  341. <code>getViewScriptPathNoControllerSpec()</code>.
  342. </para>
  343. </listitem>
  344. </itemizedlist>
  345. <para>
  346. For fine-grained control over path specifications, you may use
  347. <link linkend="zend.filter.inflector">Zend_Filter_Inflector</link>.
  348. Under the hood, the <code>ViewRenderer</code> uses an inflector to
  349. perform path mappings already. To interact with the inflector --
  350. either to set your own for use, or to modify the default inflector,
  351. the following methods may be used:
  352. </para>
  353. <itemizedlist>
  354. <listitem>
  355. <para>
  356. <code>getInflector()</code> will retrieve the inflector. If
  357. none exists yet in the <code>ViewRenderer</code>, it creates
  358. one using the default rules.
  359. </para>
  360. <para>
  361. By default, it uses static rule references for the suffix
  362. and module directory, as well as a static target; this
  363. allows various <code>ViewRenderer</code> properties the
  364. ability to dynamically modify the inflector.
  365. </para>
  366. </listitem>
  367. <listitem><para>
  368. <code>setInflector($inflector, $reference)</code> allows you
  369. to set a custom inflector for use with the
  370. <code>ViewRenderer</code>. If <varname>$reference</varname> is
  371. true, it will set the suffix and module directory as static
  372. references to <code>ViewRenderer</code> properties, as well
  373. as the target.
  374. </para></listitem>
  375. </itemizedlist>
  376. <note>
  377. <title>Default Lookup Conventions</title>
  378. <para>
  379. The <code>ViewRenderer</code> does some path normalization to
  380. make view script lookups easier. The default rules are as
  381. follows:
  382. </para>
  383. <itemizedlist>
  384. <listitem>
  385. <para>
  386. <code>:module</code>: MixedCase and camelCasedWords are separated by
  387. dashes, and the entire string cast to lowercase. E.g.:
  388. "FooBarBaz" becomes "foo-bar-baz".
  389. </para>
  390. <para>
  391. Internally, the inflector uses the filters
  392. <classname>Zend_Filter_Word_CamelCaseToDash</classname> and
  393. <classname>Zend_Filter_StringToLower</classname>.
  394. </para>
  395. </listitem>
  396. <listitem>
  397. <para>
  398. <code>:controller</code>: MixedCase and camelCasedWords are separated
  399. by dashes; underscores are converted to directory
  400. separators, and the entire string cast to lower case.
  401. Examples: "FooBar" becomes "foo-bar"; "FooBar_Admin"
  402. becomes "foo-bar/admin".
  403. </para>
  404. <para>
  405. Internally, the inflector uses the filters
  406. <classname>Zend_Filter_Word_CamelCaseToDash</classname>,
  407. <classname>Zend_Filter_Word_UnderscoreToSeparator</classname>, and
  408. <classname>Zend_Filter_StringToLower</classname>.
  409. </para>
  410. </listitem>
  411. <listitem>
  412. <para>
  413. <code>:action</code>: MixedCase and camelCasedWords are separated
  414. by dashes; non-alphanumeric characters are translated to
  415. dashes, and the entire string cast to lower case.
  416. Examples: "fooBar" becomes "foo-bar"; "foo-barBaz"
  417. becomes "foo-bar-baz".
  418. </para>
  419. <para>
  420. Internally, the inflector uses the filters
  421. <classname>Zend_Filter_Word_CamelCaseToDash</classname>,
  422. <classname>Zend_Filter_PregReplace</classname>, and
  423. <classname>Zend_Filter_StringToLower</classname>.
  424. </para>
  425. </listitem>
  426. </itemizedlist>
  427. </note>
  428. <para>
  429. The final items in the <code>ViewRenderer</code> API are the methods
  430. for actually determining view script paths and rendering views.
  431. These include:
  432. </para>
  433. <itemizedlist>
  434. <listitem>
  435. <para>
  436. <code>renderScript($script, $name)</code> allows you to
  437. render a script with a path you specify, optionally to a
  438. named path segment. When using this method, the
  439. <code>ViewRenderer</code> does no autodetermination of the
  440. script name, but instead directly passes the
  441. <varname>$script</varname> argument directly to the view object's
  442. <code>render()</code> method.
  443. </para>
  444. <note><para>
  445. Once the view has been rendered to the response object, it
  446. sets the <code>noRender</code> to prevent accidentally
  447. rendering the same view script multiple times.
  448. </para></note>
  449. <note>
  450. <para>
  451. By default,
  452. <classname>Zend_Controller_Action::renderScript()</classname>
  453. proxies to the <code>ViewRenderer</code>'s
  454. <code>renderScript()</code> method.
  455. </para>
  456. </note>
  457. </listitem>
  458. <listitem>
  459. <para>
  460. <code>getViewScript($action, $vars)</code> creates the path
  461. to a view script based on the action passed and/or any
  462. variables passed in <varname>$vars</varname>. Keys for this array
  463. may include any of the path specification keys ('moduleDir',
  464. 'module', 'controller', 'action', and 'suffix'). Any
  465. variables passed will be used; otherwise, values based on
  466. the current request will be utlized.
  467. </para>
  468. <para>
  469. <code>getViewScript()</code> will use either the
  470. <code>viewScriptPathSpec</code> or
  471. <code>viewScriptPathNoControllerSpec</code> based on the
  472. setting of the <code>noController</code> flag.
  473. </para>
  474. <para>
  475. Word delimiters occurring in module, controller, or action
  476. names will be replaced with dashes ('-'). Thus, if you have
  477. the controller name 'foo.bar' and the action 'baz:bat',
  478. using the default path specification will result in a view
  479. script path of 'foo-bar/baz-bat.phtml'.
  480. </para>
  481. <note>
  482. <para>
  483. By default,
  484. <classname>Zend_Controller_Action::getViewScript()</classname>
  485. proxies to the <code>ViewRenderer</code>'s
  486. <code>getViewScript()</code> method.
  487. </para>
  488. </note>
  489. </listitem>
  490. <listitem>
  491. <para>
  492. <code>render($action, $name, $noController)</code> checks
  493. first to see if either <varname>$name</varname> or
  494. <varname>$noController</varname> have been passed, and if so, sets
  495. the appropriate flags (responseSegment and noController,
  496. respectively) in the ViewRenderer. It then passes the
  497. <varname>$action</varname> argument, if any, on to
  498. <code>getViewScript()</code>. Finally, it passes the
  499. calculated view script path to <code>renderScript()</code>.
  500. </para>
  501. <note>
  502. <para>
  503. Be aware of the side-effects of using render(): the
  504. values you pass for the response segment name and for
  505. the noController flag will persist in the object.
  506. Additionally, noRender will be set after rendering is
  507. completed.
  508. </para>
  509. </note>
  510. <note>
  511. <para>
  512. By default,
  513. <classname>Zend_Controller_Action::render()</classname> proxies to
  514. the <code>ViewRenderer</code>'s <code>render()</code>
  515. method.
  516. </para>
  517. </note>
  518. </listitem>
  519. <listitem>
  520. <para>
  521. <code>renderBySpec($action, $vars, $name)</code> allows you
  522. to pass path specification variables in order to determine
  523. the view script path to create. It passes
  524. <varname>$action</varname> and <varname>$vars</varname> to
  525. <code>getScriptPath()</code>, and then passes the resulting
  526. script path and <varname>$name</varname> on to
  527. <code>renderScript()</code>.
  528. </para>
  529. </listitem>
  530. </itemizedlist>
  531. </sect4>
  532. <sect4 id="zend.controller.actionhelper.viewrenderer.basicusage">
  533. <title>Basic Usage Examples</title>
  534. <example id="zend.controller.actionhelper.viewrenderer.basicusage.example-1">
  535. <title>Basic Usage</title>
  536. <para>
  537. At its most basic, you simply initialize and register the
  538. <code>ViewRenderer</code> helper with the helper broker in your
  539. bootstrap, and then set variables in your action methods.
  540. </para>
  541. <programlisting language="php"><![CDATA[
  542. // In your bootstrap:
  543. Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  544. ...
  545. // 'foo' module, 'bar' controller:
  546. class Foo_BarController extends Zend_Controller_Action
  547. {
  548. // Render bar/index.phtml by default; no action required
  549. public function indexAction()
  550. {
  551. }
  552. // Render bar/populate.phtml with variable 'foo' set to 'bar'.
  553. // Since view object defined at preDispatch(), it's already available.
  554. public function populateAction()
  555. {
  556. $this->view->foo = 'bar';
  557. }
  558. // Renders nothing as it forwards to another action; the new action
  559. // will perform any rendering
  560. public function bazAction()
  561. {
  562. $this->_forward('index');
  563. }
  564. // Renders nothing as it redirects to another location
  565. public function batAction()
  566. {
  567. $this->_redirect('/index');
  568. }
  569. }
  570. ]]></programlisting>
  571. </example>
  572. <note>
  573. <title>Naming Conventions: Word Delimiters in Controller and Action Names</title>
  574. <para>
  575. If your controller or action name is composed of several
  576. words, the dispatcher requires that these are separated on
  577. the URL by specific path and word delimiter characters. The
  578. <code>ViewRenderer</code> replaces any path delimiter found
  579. in the controller name with an actual path delimiter ('/'),
  580. and any word delimiter found with a dash ('-') when creating
  581. paths. Thus, a call to the action
  582. <code>/foo.bar/baz.bat</code> would dispatch to
  583. <code>FooBarController::bazBatAction()</code> in
  584. FooBarController.php, which would render
  585. <code>foo-bar/baz-bat.phtml</code>; a call to the action
  586. <code>/bar_baz/baz-bat</code> would dispatch to
  587. <code>Bar_BazController::bazBatAction()</code> in
  588. <code>Bar/BazController.php</code> (note the path
  589. separation) and render <code>bar/baz/baz-bat.phtml</code>.
  590. </para>
  591. <para>
  592. Note that the in the second example, the module is still the
  593. default module, but that, because of the existence of a path
  594. separator, the controller receives the name
  595. <code>Bar_BazController</code>, in
  596. <code>Bar/BazController.php</code>. The ViewRenderer mimics
  597. the controller directory hierarchy.
  598. </para>
  599. </note>
  600. <example id="zend.controller.actionhelper.viewrenderer.basicusage.example-2">
  601. <title>Disabling Autorender</title>
  602. <para>
  603. For some actions or controllers, you may want to turn off the
  604. autorendering -- for instance, if you're wanting to emit a
  605. different type of output (XML, JSON, etc), or if you simply want
  606. to emit nothing. You have two options: turn off all cases of
  607. autorendering (<code>setNeverRender()</code>), or simply turn it
  608. off for the current action (<code>setNoRender()</code>).
  609. </para>
  610. <programlisting language="php"><![CDATA[
  611. // Baz controller class, bar module:
  612. class Bar_BazController extends Zend_Controller_Action
  613. {
  614. public function fooAction()
  615. {
  616. // Don't auto render this action
  617. $this->_helper->viewRenderer->setNoRender();
  618. }
  619. }
  620. // Bat controller class, bar module:
  621. class Bar_BatController extends Zend_Controller_Action
  622. {
  623. public function preDispatch()
  624. {
  625. // Never auto render this controller's actions
  626. $this->_helper->viewRenderer->setNoRender();
  627. }
  628. }
  629. ]]></programlisting>
  630. </example>
  631. <note>
  632. <para>
  633. In most cases, it makes no sense to turn off autorendering
  634. globally (ala <code>setNeverRender()</code>), as the only thing
  635. you then gain from <code>ViewRenderer</code> is the autosetup of
  636. the view object.
  637. </para>
  638. </note>
  639. <example id="zend.controller.actionhelper.viewrenderer.basicusage.example-3">
  640. <title>Choosing a Different View Script</title>
  641. <para>
  642. Some situations require that you render a different script than
  643. one named after the action. For instance, if you have a
  644. controller that has both add and edit actions, they may both
  645. display the same 'form' view, albeit with different values set.
  646. You can easily change the script name used with either
  647. <code>setScriptAction()</code>, <code>setRender()</code>, or
  648. calling the helper as a method, which will invoke
  649. <code>setRender()</code>.
  650. </para>
  651. <programlisting language="php"><![CDATA[
  652. // Bar controller class, foo module:
  653. class Foo_BarController extends Zend_Controller_Action
  654. {
  655. public function addAction()
  656. {
  657. // Render 'bar/form.phtml' instead of 'bar/add.phtml'
  658. $this->_helper->viewRenderer('form');
  659. }
  660. public function editAction()
  661. {
  662. // Render 'bar/form.phtml' instead of 'bar/edit.phtml'
  663. $this->_helper->viewRenderer->setScriptAction('form');
  664. }
  665. public function processAction()
  666. {
  667. // do some validation...
  668. if (!$valid) {
  669. // Render 'bar/form.phtml' instead of 'bar/process.phtml'
  670. $this->_helper->viewRenderer->setRender('form');
  671. return;
  672. }
  673. // otherwise continue processing...
  674. }
  675. }
  676. ]]></programlisting>
  677. </example>
  678. <example id="zend.controller.actionhelper.viewrenderer.basicusage.example-4">
  679. <title>Modifying the Registered View</title>
  680. <para>
  681. What if you need to modify the view object -- for instance,
  682. change the helper paths, or the encoding? You can do so either
  683. by modifying the view object set in your controller, or by
  684. grabbing the view object out of the <code>ViewRenderer</code>;
  685. both are references to the same object.
  686. </para>
  687. <programlisting language="php"><![CDATA[
  688. // Bar controller class, foo module:
  689. class Foo_BarController extends Zend_Controller_Action
  690. {
  691. public function preDispatch()
  692. {
  693. // change view encoding
  694. $this->view->setEncoding('UTF-8');
  695. }
  696. public function bazAction()
  697. {
  698. // Get view object and set escape callback to 'htmlspecialchars'
  699. $view = $this->_helper->viewRenderer->view;
  700. $view->setEscape('htmlspecialchars');
  701. }
  702. }
  703. ]]></programlisting>
  704. </example>
  705. </sect4>
  706. <sect4 id="zend.controller.actionhelper.viewrenderer.advancedusage">
  707. <title>Advanced Usage Examples</title>
  708. <example id="zend.controller.actionhelper.viewrenderer.advancedusage.example-1">
  709. <title>Changing the Path Specifications</title>
  710. <para>
  711. In some circumstances, you may decide that the default path
  712. specifications do not fit your site's needs. For instance, you
  713. may want to have a single template tree to which you may then
  714. give access to your designers (this is very typical when using
  715. <ulink url="http://smarty.php.net/">Smarty</ulink>, for
  716. instance). In such a case, you may want to hardcode the view
  717. base path specification, and create an alternate specification
  718. for the action view script paths themselves.
  719. </para>
  720. <para>
  721. For purposes of this example, let's assume that the base path to
  722. views should be '/opt/vendor/templates', and that you wish for
  723. view scripts to be referenced by
  724. ':moduleDir/:controller/:action.:suffix'; if the noController
  725. flag has been set, you want to render out of the top level
  726. instead of in a subdirectory (':action.:suffix'). Finally, you
  727. want to use 'tpl' as the view script filename suffix.
  728. </para>
  729. <programlisting language="php"><![CDATA[
  730. /**
  731. * In your bootstrap:
  732. */
  733. // Different view implementation
  734. $view = new ZF_Smarty();
  735. $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
  736. $viewRenderer->setViewBasePathSpec('/opt/vendor/templates')
  737. ->setViewScriptPathSpec(':module/:controller/:action.:suffix')
  738. ->setViewScriptPathNoControllerSpec(':action.:suffix')
  739. ->setViewSuffix('tpl');
  740. Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
  741. ]]></programlisting>
  742. </example>
  743. <example id="zend.controller.actionhelper.viewrenderer.advancedusage.example-2">
  744. <title>Rendering Multiple View Scripts from a Single Action</title>
  745. <para>
  746. At times, you may need to render multiple view scripts from a
  747. single action. This is very straightforward -- simply make
  748. multiple calls to <code>render()</code>:
  749. </para>
  750. <programlisting language="php"><![CDATA[
  751. class SearchController extends Zend_Controller_Action
  752. {
  753. public function resultsAction()
  754. {
  755. // Assume $this->model is the current model
  756. $this->view->results =
  757. $this->model->find($this->_getParam('query', '');
  758. // render() by default proxies to the ViewRenderer
  759. // Render first the search form and then the results
  760. $this->render('form');
  761. $this->render('results');
  762. }
  763. public function formAction()
  764. {
  765. // do nothing; ViewRenderer autorenders the view script
  766. }
  767. }
  768. ]]></programlisting>
  769. </example>
  770. </sect4>
  771. </sect3>
  772. <!--
  773. vim:se ts=4 sw=4 et:
  774. -->