Zend_View-Scripts.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.view.scripts">
  4. <title>View Scripts</title>
  5. <para>
  6. Once your controller has assigned variables and called <code>render()</code>,
  7. <classname>Zend_View</classname> then includes the requested view script and executes
  8. it "inside" the scope of the <classname>Zend_View</classname> instance. Therefore,
  9. in your view scripts, references to $this actually point to the
  10. <classname>Zend_View</classname> instance itself.
  11. </para>
  12. <para>
  13. Variables assigned to the view from the controller are referred
  14. to as instance properties. For example, if the controller were
  15. to assign a variable 'something', you would refer to it as
  16. $this->something in the view script. (This allows you to keep
  17. track of which values were assigned to the script, and which are
  18. internal to the script itself.)
  19. </para>
  20. <para>
  21. By way of reminder, here is the example view script from the
  22. <classname>Zend_View</classname> introduction.
  23. </para>
  24. <programlisting role="php"><![CDATA[
  25. <?php if ($this->books): ?>
  26. <!-- A table of some books. -->
  27. <table>
  28. <tr>
  29. <th>Author</th>
  30. <th>Title</th>
  31. </tr>
  32. <?php foreach ($this->books as $key => $val): ?>
  33. <tr>
  34. <td><?php echo $this->escape($val['author']) ?></td>
  35. <td><?php echo $this->escape($val['title']) ?></td>
  36. </tr>
  37. <?php endforeach; ?>
  38. </table>
  39. <?php else: ?>
  40. <p>There are no books to display.</p>
  41. <?php endif;?>
  42. ]]></programlisting>
  43. <sect2 id="zend.view.scripts.escaping">
  44. <title>Escaping Output</title>
  45. <para>
  46. One of the most important tasks to perform in a view script
  47. is to make sure that output is escaped properly; among other
  48. things, this helps to avoid cross-site scripting attacks.
  49. Unless you are using a function, method, or helper that does
  50. escaping on its own, you should always escape variables when
  51. you output them.
  52. </para>
  53. <para>
  54. <classname>Zend_View</classname> comes with a method called escape() that does such
  55. escaping for you.
  56. </para>
  57. <programlisting role="php"><![CDATA[
  58. // bad view-script practice:
  59. echo $this->variable;
  60. // good view-script practice:
  61. echo $this->escape($this->variable);
  62. ]]></programlisting>
  63. <para>
  64. By default, the escape() method uses the PHP htmlspecialchars()
  65. function for escaping. However, depending on your environment,
  66. you may wish for escaping to occur in a different way. Use the
  67. setEscape() method at the controller level to tell <classname>Zend_View</classname>
  68. what escaping callback to use.
  69. </para>
  70. <programlisting role="php"><![CDATA[
  71. // create a Zend_View instance
  72. $view = new Zend_View();
  73. // tell it to use htmlentities as the escaping callback
  74. $view->setEscape('htmlentities');
  75. // or tell it to use a static class method as the callback
  76. $view->setEscape(array('SomeClass', 'methodName'));
  77. // or even an instance method
  78. $obj = new SomeClass();
  79. $view->setEscape(array($obj, 'methodName'));
  80. // and then render your view
  81. echo $view->render(...);
  82. ]]></programlisting>
  83. <para>
  84. The callback function or method should take the value to be
  85. escaped as its first parameter, and all other parameters should
  86. be optional.
  87. </para>
  88. </sect2>
  89. <sect2 id="zend.view.scripts.templates">
  90. <title>Using Alternate Template Systems</title>
  91. <para>
  92. Although PHP is itself a powerful template system, many developers
  93. feel it is too powerful or complex for their template designers and
  94. will want to use an alternate template engine. <classname>Zend_View</classname> provides
  95. two mechanisms for doing so, the first through view scripts, the
  96. second by implementing <classname>Zend_View_Interface</classname>.
  97. </para>
  98. <sect3 id="zend.view.scripts.templates.scripts">
  99. <title>Template Systems Using View Scripts</title>
  100. <para>
  101. A view script may be used to instantiate and manipulate a
  102. separate template object, such as a PHPLIB-style template. The
  103. view script for that kind of activity might look something like
  104. this:
  105. </para>
  106. <programlisting role="php"><![CDATA[
  107. include_once 'template.inc';
  108. $tpl = new Template();
  109. if ($this->books) {
  110. $tpl->setFile(array(
  111. "booklist" => "booklist.tpl",
  112. "eachbook" => "eachbook.tpl",
  113. ));
  114. foreach ($this->books as $key => $val) {
  115. $tpl->set_var('author', $this->escape($val['author']);
  116. $tpl->set_var('title', $this->escape($val['title']);
  117. $tpl->parse("books", "eachbook", true);
  118. }
  119. $tpl->pparse("output", "booklist");
  120. } else {
  121. $tpl->setFile("nobooks", "nobooks.tpl")
  122. $tpl->pparse("output", "nobooks");
  123. }
  124. ]]></programlisting>
  125. <para>
  126. These would be the related template files:
  127. </para>
  128. <programlisting role="html"><![CDATA[
  129. <!-- booklist.tpl -->
  130. <table>
  131. <tr>
  132. <th>Author</th>
  133. <th>Title</th>
  134. </tr>
  135. {books}
  136. </table>
  137. <!-- eachbook.tpl -->
  138. <tr>
  139. <td>{author}</td>
  140. <td>{title}</td>
  141. </tr>
  142. <!-- nobooks.tpl -->
  143. <p>There are no books to display.</p>
  144. ]]></programlisting>
  145. </sect3>
  146. <sect3 id="zend.view.scripts.templates.interface">
  147. <title>Template Systems Using Zend_View_Interface</title>
  148. <para>
  149. Some may find it easier to simply provide a <classname>Zend_View</classname>-compatible
  150. template engine. <classname>Zend_View_Interface</classname> defines the
  151. minimum interface needed for compatability:
  152. </para>
  153. <programlisting role="php"><![CDATA[
  154. /**
  155. * Return the actual template engine object
  156. */
  157. public function getEngine();
  158. /**
  159. * Set the path to view scripts/templates
  160. */
  161. public function setScriptPath($path);
  162. /**
  163. * Set a base path to all view resources
  164. */
  165. public function setBasePath($path, $prefix = 'Zend_View');
  166. /**
  167. * Add an additional base path to view resources
  168. */
  169. public function addBasePath($path, $prefix = 'Zend_View');
  170. /**
  171. * Retrieve the current script paths
  172. */
  173. public function getScriptPaths();
  174. /**
  175. * Overloading methods for assigning template variables as object
  176. * properties
  177. */
  178. public function __set($key, $value);
  179. public function __isset($key);
  180. public function __unset($key);
  181. /**
  182. * Manual assignment of template variables, or ability to assign
  183. * multiple variables en masse.
  184. */
  185. public function assign($spec, $value = null);
  186. /**
  187. * Unset all assigned template variables
  188. */
  189. public function clearVars();
  190. /**
  191. * Render the template named $name
  192. */
  193. public function render($name);
  194. ]]></programlisting>
  195. <para>
  196. Using this interface, it becomes relatively easy to wrap a
  197. third-party template engine as a <classname>Zend_View</classname>-compatible class. As
  198. an example, the following is one potential wrapper for Smarty:
  199. </para>
  200. <programlisting role="php"><![CDATA[
  201. class Zend_View_Smarty implements Zend_View_Interface
  202. {
  203. /**
  204. * Smarty object
  205. * @var Smarty
  206. */
  207. protected $_smarty;
  208. /**
  209. * Constructor
  210. *
  211. * @param string $tmplPath
  212. * @param array $extraParams
  213. * @return void
  214. */
  215. public function __construct($tmplPath = null, $extraParams = array())
  216. {
  217. $this->_smarty = new Smarty;
  218. if (null !== $tmplPath) {
  219. $this->setScriptPath($tmplPath);
  220. }
  221. foreach ($extraParams as $key => $value) {
  222. $this->_smarty->$key = $value;
  223. }
  224. }
  225. /**
  226. * Return the template engine object
  227. *
  228. * @return Smarty
  229. */
  230. public function getEngine()
  231. {
  232. return $this->_smarty;
  233. }
  234. /**
  235. * Set the path to the templates
  236. *
  237. * @param string $path The directory to set as the path.
  238. * @return void
  239. */
  240. public function setScriptPath($path)
  241. {
  242. if (is_readable($path)) {
  243. $this->_smarty->template_dir = $path;
  244. return;
  245. }
  246. throw new Exception('Invalid path provided');
  247. }
  248. /**
  249. * Retrieve the current template directory
  250. *
  251. * @return string
  252. */
  253. public function getScriptPaths()
  254. {
  255. return array($this->_smarty->template_dir);
  256. }
  257. /**
  258. * Alias for setScriptPath
  259. *
  260. * @param string $path
  261. * @param string $prefix Unused
  262. * @return void
  263. */
  264. public function setBasePath($path, $prefix = 'Zend_View')
  265. {
  266. return $this->setScriptPath($path);
  267. }
  268. /**
  269. * Alias for setScriptPath
  270. *
  271. * @param string $path
  272. * @param string $prefix Unused
  273. * @return void
  274. */
  275. public function addBasePath($path, $prefix = 'Zend_View')
  276. {
  277. return $this->setScriptPath($path);
  278. }
  279. /**
  280. * Assign a variable to the template
  281. *
  282. * @param string $key The variable name.
  283. * @param mixed $val The variable value.
  284. * @return void
  285. */
  286. public function __set($key, $val)
  287. {
  288. $this->_smarty->assign($key, $val);
  289. }
  290. /**
  291. * Allows testing with empty() and isset() to work
  292. *
  293. * @param string $key
  294. * @return boolean
  295. */
  296. public function __isset($key)
  297. {
  298. return (null !== $this->_smarty->get_template_vars($key));
  299. }
  300. /**
  301. * Allows unset() on object properties to work
  302. *
  303. * @param string $key
  304. * @return void
  305. */
  306. public function __unset($key)
  307. {
  308. $this->_smarty->clear_assign($key);
  309. }
  310. /**
  311. * Assign variables to the template
  312. *
  313. * Allows setting a specific key to the specified value, OR passing
  314. * an array of key => value pairs to set en masse.
  315. *
  316. * @see __set()
  317. * @param string|array $spec The assignment strategy to use (key or
  318. * array of key => value pairs)
  319. * @param mixed $value (Optional) If assigning a named variable,
  320. * use this as the value.
  321. * @return void
  322. */
  323. public function assign($spec, $value = null)
  324. {
  325. if (is_array($spec)) {
  326. $this->_smarty->assign($spec);
  327. return;
  328. }
  329. $this->_smarty->assign($spec, $value);
  330. }
  331. /**
  332. * Clear all assigned variables
  333. *
  334. * Clears all variables assigned to Zend_View either via
  335. * {@link assign()} or property overloading
  336. * ({@link __get()}/{@link __set()}).
  337. *
  338. * @return void
  339. */
  340. public function clearVars()
  341. {
  342. $this->_smarty->clear_all_assign();
  343. }
  344. /**
  345. * Processes a template and returns the output.
  346. *
  347. * @param string $name The template to process.
  348. * @return string The output.
  349. */
  350. public function render($name)
  351. {
  352. return $this->_smarty->fetch($name);
  353. }
  354. }
  355. ]]></programlisting>
  356. <para>
  357. In this example, you would instantiate the
  358. <classname>Zend_View_Smarty</classname> class instead of
  359. <classname>Zend_View</classname>, and then use it in roughly the same
  360. fashion as <classname>Zend_View</classname>:
  361. </para>
  362. <programlisting role="php"><![CDATA[
  363. //Example 1. In initView() of initializer.
  364. $view = new Zend_View_Smarty('/path/to/templates');
  365. $viewRenderer =
  366. new Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
  367. $viewRenderer->setView($view)
  368. ->setViewBasePathSpec($view->_smarty->template_dir)
  369. ->setViewScriptPathSpec(':controller/:action.:suffix')
  370. ->setViewScriptPathNoControllerSpec(':action.:suffix')
  371. ->setViewSuffix('tpl');
  372. //Example 2. Usage in action controller remains the same...
  373. class FooController extends Zend_Controller_Action
  374. {
  375. public function barAction()
  376. {
  377. $this->view->book = 'Zend PHP 5 Certification Study Guide';
  378. $this->view->author = 'Davey Shafik and Ben Ramsey'
  379. }
  380. }
  381. //Example 3. Initializing view in action controller
  382. class FooController extends Zend_Controller_Action
  383. {
  384. public function init()
  385. {
  386. $this->view = new Zend_View_Smarty('/path/to/templates');
  387. $viewRenderer = $this->_helper->getHelper('viewRenderer');
  388. $viewRenderer->setView($this->view)
  389. ->setViewBasePathSpec($view->_smarty->template_dir)
  390. ->setViewScriptPathSpec(':controller/:action.:suffix')
  391. ->setViewScriptPathNoControllerSpec(':action.:suffix')
  392. ->setViewSuffix('tpl');
  393. }
  394. ]]></programlisting>
  395. </sect3>
  396. </sect2>
  397. </sect1>
  398. <!--
  399. vim:se ts=4 sw=4 et:
  400. -->