Zend_View-Scripts.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 <methodname>render()</methodname>,
  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 language="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 language="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 <acronym>PHP</acronym> 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 language="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 <acronym>PHP</acronym> 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 language="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 language="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
  150. <classname>Zend_View</classname>-compatible template engine.
  151. <classname>Zend_View_Interface</classname> defines the minimum interface needed for
  152. compatability:
  153. </para>
  154. <programlisting language="php"><![CDATA[
  155. /**
  156. * Return the actual template engine object
  157. */
  158. public function getEngine();
  159. /**
  160. * Set the path to view scripts/templates
  161. */
  162. public function setScriptPath($path);
  163. /**
  164. * Set a base path to all view resources
  165. */
  166. public function setBasePath($path, $prefix = 'Zend_View');
  167. /**
  168. * Add an additional base path to view resources
  169. */
  170. public function addBasePath($path, $prefix = 'Zend_View');
  171. /**
  172. * Retrieve the current script paths
  173. */
  174. public function getScriptPaths();
  175. /**
  176. * Overloading methods for assigning template variables as object
  177. * properties
  178. */
  179. public function __set($key, $value);
  180. public function __isset($key);
  181. public function __unset($key);
  182. /**
  183. * Manual assignment of template variables, or ability to assign
  184. * multiple variables en masse.
  185. */
  186. public function assign($spec, $value = null);
  187. /**
  188. * Unset all assigned template variables
  189. */
  190. public function clearVars();
  191. /**
  192. * Render the template named $name
  193. */
  194. public function render($name);
  195. ]]></programlisting>
  196. <para>
  197. Using this interface, it becomes relatively easy to wrap a
  198. third-party template engine as a <classname>Zend_View</classname>-compatible class.
  199. As an example, the following is one potential wrapper for Smarty:
  200. </para>
  201. <programlisting language="php"><![CDATA[
  202. class Zend_View_Smarty implements Zend_View_Interface
  203. {
  204. /**
  205. * Smarty object
  206. * @var Smarty
  207. */
  208. protected $_smarty;
  209. /**
  210. * Constructor
  211. *
  212. * @param string $tmplPath
  213. * @param array $extraParams
  214. * @return void
  215. */
  216. public function __construct($tmplPath = null, $extraParams = array())
  217. {
  218. $this->_smarty = new Smarty;
  219. if (null !== $tmplPath) {
  220. $this->setScriptPath($tmplPath);
  221. }
  222. foreach ($extraParams as $key => $value) {
  223. $this->_smarty->$key = $value;
  224. }
  225. }
  226. /**
  227. * Return the template engine object
  228. *
  229. * @return Smarty
  230. */
  231. public function getEngine()
  232. {
  233. return $this->_smarty;
  234. }
  235. /**
  236. * Set the path to the templates
  237. *
  238. * @param string $path The directory to set as the path.
  239. * @return void
  240. */
  241. public function setScriptPath($path)
  242. {
  243. if (is_readable($path)) {
  244. $this->_smarty->template_dir = $path;
  245. return;
  246. }
  247. throw new Exception('Invalid path provided');
  248. }
  249. /**
  250. * Retrieve the current template directory
  251. *
  252. * @return string
  253. */
  254. public function getScriptPaths()
  255. {
  256. return array($this->_smarty->template_dir);
  257. }
  258. /**
  259. * Alias for setScriptPath
  260. *
  261. * @param string $path
  262. * @param string $prefix Unused
  263. * @return void
  264. */
  265. public function setBasePath($path, $prefix = 'Zend_View')
  266. {
  267. return $this->setScriptPath($path);
  268. }
  269. /**
  270. * Alias for setScriptPath
  271. *
  272. * @param string $path
  273. * @param string $prefix Unused
  274. * @return void
  275. */
  276. public function addBasePath($path, $prefix = 'Zend_View')
  277. {
  278. return $this->setScriptPath($path);
  279. }
  280. /**
  281. * Assign a variable to the template
  282. *
  283. * @param string $key The variable name.
  284. * @param mixed $val The variable value.
  285. * @return void
  286. */
  287. public function __set($key, $val)
  288. {
  289. $this->_smarty->assign($key, $val);
  290. }
  291. /**
  292. * Allows testing with empty() and isset() to work
  293. *
  294. * @param string $key
  295. * @return boolean
  296. */
  297. public function __isset($key)
  298. {
  299. return (null !== $this->_smarty->get_template_vars($key));
  300. }
  301. /**
  302. * Allows unset() on object properties to work
  303. *
  304. * @param string $key
  305. * @return void
  306. */
  307. public function __unset($key)
  308. {
  309. $this->_smarty->clear_assign($key);
  310. }
  311. /**
  312. * Assign variables to the template
  313. *
  314. * Allows setting a specific key to the specified value, OR passing
  315. * an array of key => value pairs to set en masse.
  316. *
  317. * @see __set()
  318. * @param string|array $spec The assignment strategy to use (key or
  319. * array of key => value pairs)
  320. * @param mixed $value (Optional) If assigning a named variable,
  321. * use this as the value.
  322. * @return void
  323. */
  324. public function assign($spec, $value = null)
  325. {
  326. if (is_array($spec)) {
  327. $this->_smarty->assign($spec);
  328. return;
  329. }
  330. $this->_smarty->assign($spec, $value);
  331. }
  332. /**
  333. * Clear all assigned variables
  334. *
  335. * Clears all variables assigned to Zend_View either via
  336. * {@link assign()} or property overloading
  337. * ({@link __get()}/{@link __set()}).
  338. *
  339. * @return void
  340. */
  341. public function clearVars()
  342. {
  343. $this->_smarty->clear_all_assign();
  344. }
  345. /**
  346. * Processes a template and returns the output.
  347. *
  348. * @param string $name The template to process.
  349. * @return string The output.
  350. */
  351. public function render($name)
  352. {
  353. return $this->_smarty->fetch($name);
  354. }
  355. }
  356. ]]></programlisting>
  357. <para>
  358. In this example, you would instantiate the
  359. <classname>Zend_View_Smarty</classname> class instead of
  360. <classname>Zend_View</classname>, and then use it in roughly the same
  361. fashion as <classname>Zend_View</classname>:
  362. </para>
  363. <programlisting language="php"><![CDATA[
  364. //Example 1. In initView() of initializer.
  365. $view = new Zend_View_Smarty('/path/to/templates');
  366. $viewRenderer =
  367. Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
  368. $viewRenderer->setView($view)
  369. ->setViewBasePathSpec($view->_smarty->template_dir)
  370. ->setViewScriptPathSpec(':controller/:action.:suffix')
  371. ->setViewScriptPathNoControllerSpec(':action.:suffix')
  372. ->setViewSuffix('tpl');
  373. //Example 2. Usage in action controller remains the same...
  374. class FooController extends Zend_Controller_Action
  375. {
  376. public function barAction()
  377. {
  378. $this->view->book = 'Zend PHP 5 Certification Study Guide';
  379. $this->view->author = 'Davey Shafik and Ben Ramsey'
  380. }
  381. }
  382. //Example 3. Initializing view in action controller
  383. class FooController extends Zend_Controller_Action
  384. {
  385. public function init()
  386. {
  387. $this->view = new Zend_View_Smarty('/path/to/templates');
  388. $viewRenderer = $this->_helper->getHelper('viewRenderer');
  389. $viewRenderer->setView($this->view)
  390. ->setViewBasePathSpec($view->_smarty->template_dir)
  391. ->setViewScriptPathSpec(':controller/:action.:suffix')
  392. ->setViewScriptPathNoControllerSpec(':action.:suffix')
  393. ->setViewSuffix('tpl');
  394. }
  395. ]]></programlisting>
  396. </sect3>
  397. </sect2>
  398. </sect1>
  399. <!--
  400. vim:se ts=4 sw=4 et:
  401. -->