Zend_View-Scripts.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <!-- EN-Revision: 24249 -->
  4. <sect1 id="zend.view.scripts">
  5. <title>ビュースクリプト</title>
  6. <para>
  7. コントローラが変数を代入して <methodname>render()</methodname> をコールすると、
  8. 指定されたビュースクリプトを <classname>Zend_View</classname> が読み込み、<classname>Zend_View</classname>
  9. インスタンスのスコープでそれを実行します。したがって、
  10. ビュースクリプトの中で $this を参照すると、
  11. 実際には <classname>Zend_View</classname> のインスタンスを指すことになります。
  12. </para>
  13. <para>
  14. コントローラからビューに代入された変数は、
  15. ビューインスタンスのプロパティとして参照できます。例えば、
  16. コントローラで変数 'something' を代入したとすると、
  17. ビュースクリプト内ではそれを $this->something で取得できます
  18. (これにより、どの値がコントローラから代入されたもので、
  19. どの値がスクリプト内部で作成されたものなのかを追いかけられるようになります)。
  20. </para>
  21. <para>
  22. <classname>Zend_View</classname> の導入の部分で示したビュースクリプトの例を思い出してみましょう。
  23. </para>
  24. <programlisting language="php"><![CDATA[
  25. <?php if ($this->books): ?>
  26. <!-- 本の一覧 -->
  27. <table>
  28. <tr>
  29. <th>著者</th>
  30. <th>タイトル</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>表示する本がありません。</p>
  41. <?php endif;?>
  42. ]]></programlisting>
  43. <sect2 id="zend.view.scripts.escaping">
  44. <title>出力のエスケープ</title>
  45. <para>
  46. ビュースクリプトで行うべき仕事のうち最も重要なもののひとつは、
  47. 出力を適切にエスケープすることです。これは、
  48. クロスサイトスクリプティング攻撃を防ぐのを助けます。
  49. それ自身がエスケープを行ってくれるような関数、メソッド、
  50. あるいはヘルパーを使用しているのでない限り、
  51. 変数を出力する際には常にそれをエスケープしなければなりません。
  52. </para>
  53. <para>
  54. <classname>Zend_View</classname> の escape() というメソッドが、このエスケープを行います。
  55. </para>
  56. <programlisting language="php"><![CDATA[
  57. // ビュースクリプトの悪い例
  58. echo $this->variable;
  59. // ビュースクリプトのよい例
  60. echo $this->escape($this->variable);
  61. ]]></programlisting>
  62. <para>
  63. デフォルトでは、escape() メソッドは <acronym>PHP</acronym> の htmlspecialchars()
  64. 関数でエスケープを行います。しかし環境によっては、
  65. 別の方法でエスケープしたくなることもあるでしょう。
  66. コントローラから setEscape() メソッドを実行することで、
  67. エスケープに使用するコールバックを <classname>Zend_View</classname> に通知できます。
  68. </para>
  69. <programlisting language="php"><![CDATA[
  70. // Zend_View のインスタンスを作成します
  71. $view = new Zend_View();
  72. // エスケープに htmlentities を使用するように通知します
  73. $view->setEscape('htmlentities');
  74. // あるいは、クラスの静的メソッドを使用するように通知します
  75. $view->setEscape(array('SomeClass', 'methodName'));
  76. // あるいは、インスタンスメソッドを指定することもできます
  77. $obj = new SomeClass();
  78. $view->setEscape(array($obj, 'methodName'));
  79. // そして、ビューをレンダリングします
  80. echo $view->render(...);
  81. ]]></programlisting>
  82. <para>
  83. コールバック関数あるいはメソッドは、
  84. エスケープする値を最初のパラメータとして受け取ります。
  85. それ以外のパラメータはオプションとなります。
  86. </para>
  87. </sect2>
  88. <sect2 id="zend.view.scripts.templates">
  89. <title>別のテンプレートシステムの使用</title>
  90. <para>
  91. <acronym>PHP</acronym> 自身も強力なテンプレートシステムではありますが、
  92. 開発者の多くは、デザイナにとっては高機能すぎる/複雑すぎる
  93. と感じており、別のテンプレートエンジンをほしがっているようです。
  94. <classname>Zend_View</classname> では、そのような目的のために二種類の仕組みを提供します。
  95. ビュースクリプトを使用することによるものと、
  96. <classname>Zend_View_Interface</classname> 実装することによるものです。
  97. </para>
  98. <sect3 id="zend.view.scripts.templates.scripts">
  99. <title>ビュースクリプトを使用したテンプレートシステム</title>
  100. <para>
  101. ビュースクリプトを使用して、PHPLIB 形式のテンプレートのような
  102. 別のテンプレートオブジェクトのインスタンスを作成し、
  103. それを操作できます。ビュースクリプトをこのように使用する方法は、
  104. 以下のようになります。
  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. 関連するテンプレートファイルは、このようになります。
  127. </para>
  128. <programlisting language="html"><![CDATA[
  129. <!-- booklist.tpl -->
  130. <table>
  131. <tr>
  132. <th>著者</th>
  133. <th>タイトル</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>表示する本がありません。</p>
  144. ]]></programlisting>
  145. </sect3>
  146. <sect3 id="zend.view.scripts.templates.interface">
  147. <title>Zend_View_Interface を使用したテンプレート</title>
  148. <para>
  149. <classname>Zend_View</classname> 互換のテンプレートエンジンを使用するほうが簡単だという人もいるでしょう。
  150. <classname>Zend_View_Interface</classname> では、
  151. 互換性を保つために最低限必要なインターフェイスを定義しています。
  152. </para>
  153. <programlisting language="php"><![CDATA[
  154. /**
  155. * テンプレートエンジンオブジェクトを返します
  156. */
  157. public function getEngine();
  158. /**
  159. * ビュースクリプト/テンプレートへのパスを設定します
  160. */
  161. public function setScriptPath($path);
  162. /**
  163. * すべてのビューリソースへのベースパスを設定します
  164. */
  165. public function setBasePath($path, $prefix = 'Zend_View');
  166. /**
  167. * ビューリソースへのベースパスを追加します
  168. */
  169. public function addBasePath($path, $prefix = 'Zend_View');
  170. /**
  171. * 現在のスクリプトのパスを取得します
  172. */
  173. public function getScriptPaths();
  174. /**
  175. * テンプレート変数をオブジェクトのプロパティとして代入するためのオーバーロードメソッド
  176. */
  177. public function __set($key, $value);
  178. public function __isset($key);
  179. public function __unset($key);
  180. /**
  181. * テンプレート変数を手動で代入したり、複数の変数を
  182. * 一括設定したりします
  183. */
  184. public function assign($spec, $value = null);
  185. /**
  186. * 代入済みのテンプレート変数を削除します
  187. */
  188. public function clearVars();
  189. /**
  190. * $name というテンプレートをレンダリングします
  191. */
  192. public function render($name);
  193. ]]></programlisting>
  194. <para>
  195. このインターフェイスを使用すると、
  196. サードパーティのテンプレートエンジンをラップして
  197. <classname>Zend_View</classname> 互換のクラスを作成することが簡単になります。
  198. 例として、Smarty 用のラッパーはこのようになります。
  199. </para>
  200. <programlisting language="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. * コンストラクタ
  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. * テンプレートエンジンオブジェクトを返します
  227. *
  228. * @return Smarty
  229. */
  230. public function getEngine()
  231. {
  232. return $this->_smarty;
  233. }
  234. /**
  235. * テンプレートへのパスを設定します
  236. *
  237. * @param string $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('無効なパスが指定されました');
  247. }
  248. /**
  249. * 現在のテンプレートディレクトリを取得します
  250. *
  251. * @return string
  252. */
  253. public function getScriptPaths()
  254. {
  255. return array($this->_smarty->template_dir);
  256. }
  257. /**
  258. * 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. * 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. * 変数をテンプレートに代入します
  281. *
  282. * @param string $key 変数名
  283. * @param mixed $val 変数の値
  284. * @return void
  285. */
  286. public function __set($key, $val)
  287. {
  288. $this->_smarty->assign($key, $val);
  289. }
  290. /**
  291. * empty() や isset() のテストが動作するようにします
  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. * オブジェクトのプロパティに対して unset() が動作するようにします
  302. *
  303. * @param string $key
  304. * @return void
  305. */
  306. public function __unset($key)
  307. {
  308. $this->_smarty->clear_assign($key);
  309. }
  310. /**
  311. * 変数をテンプレートに代入します
  312. *
  313. * 指定したキーを指定した値に設定します。あるいは、
  314. * キー => 値 形式の配列で一括設定します
  315. *
  316. * @see __set()
  317. * @param string|array $spec 使用する代入方式 (キー、あるいは キー => 値 の配列)
  318. * @param mixed $value (オプション) 名前を指定して代入する場合は、ここで値を指定します
  319. * @return void
  320. */
  321. public function assign($spec, $value = null)
  322. {
  323. if (is_array($spec)) {
  324. $this->_smarty->assign($spec);
  325. return;
  326. }
  327. $this->_smarty->assign($spec, $value);
  328. }
  329. /**
  330. * 代入済みのすべての変数を削除します
  331. *
  332. * Zend_View に {@link assign()} やプロパティ
  333. * ({@link __get()}/{@link __set()}) で代入された変数をすべて削除します
  334. *
  335. * @return void
  336. */
  337. public function clearVars()
  338. {
  339. $this->_smarty->clear_all_assign();
  340. }
  341. /**
  342. * テンプレートを処理し、結果を出力します
  343. *
  344. * @param string $name 処理するテンプレート
  345. * @return string 出力結果
  346. */
  347. public function render($name)
  348. {
  349. return $this->_smarty->fetch($name);
  350. }
  351. }
  352. ]]></programlisting>
  353. <para>
  354. この例では、<classname>Zend_View</classname> ではなく
  355. <classname>Zend_View_Smarty</classname> クラスのインスタンスを作成し、
  356. それを使用して <classname>Zend_View</classname> と同じようなことをしています。
  357. </para>
  358. <programlisting language="php"><![CDATA[
  359. //例 1. InitializerのinitView()で
  360. $view = new Zend_View_Smarty('/path/to/templates');
  361. $viewRenderer =
  362. Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
  363. $viewRenderer->setView($view)
  364. ->setViewBasePathSpec($view->_smarty->template_dir)
  365. ->setViewScriptPathSpec(':controller/:action.:suffix')
  366. ->setViewScriptPathNoControllerSpec(':action.:suffix')
  367. ->setViewSuffix('tpl');
  368. //例 2. アクションコントローラでも同様に...
  369. class FooController extends Zend_Controller_Action
  370. {
  371. public function barAction()
  372. {
  373. $this->view->book = 'Zend PHP 5 Certification Study Guide';
  374. $this->view->author = 'Davey Shafik and Ben Ramsey'
  375. }
  376. }
  377. //例 3. アクションコントローラでのビューの初期化
  378. class FooController extends Zend_Controller_Action
  379. {
  380. public function init()
  381. {
  382. $this->view = new Zend_View_Smarty('/path/to/templates');
  383. $viewRenderer = $this->_helper->getHelper('viewRenderer');
  384. $viewRenderer->setView($this->view)
  385. ->setViewBasePathSpec($view->_smarty->template_dir)
  386. ->setViewScriptPathSpec(':controller/:action.:suffix')
  387. ->setViewScriptPathNoControllerSpec(':action.:suffix')
  388. ->setViewSuffix('tpl');
  389. }
  390. }
  391. ]]></programlisting>
  392. </sect3>
  393. </sect2>
  394. </sect1>
  395. <!--
  396. vim:se ts=4 sw=4 et:
  397. -->