| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- Reviewed: no -->
- <!-- EN-Revision: 24249 -->
- <sect1 id="zend.view.scripts">
- <title>ビュースクリプト</title>
- <para>
- コントローラが変数を代入して <methodname>render()</methodname> をコールすると、
- 指定されたビュースクリプトを <classname>Zend_View</classname> が読み込み、<classname>Zend_View</classname>
- インスタンスのスコープでそれを実行します。したがって、
- ビュースクリプトの中で $this を参照すると、
- 実際には <classname>Zend_View</classname> のインスタンスを指すことになります。
- </para>
- <para>
- コントローラからビューに代入された変数は、
- ビューインスタンスのプロパティとして参照できます。例えば、
- コントローラで変数 'something' を代入したとすると、
- ビュースクリプト内ではそれを $this->something で取得できます
- (これにより、どの値がコントローラから代入されたもので、
- どの値がスクリプト内部で作成されたものなのかを追いかけられるようになります)。
- </para>
- <para>
- <classname>Zend_View</classname> の導入の部分で示したビュースクリプトの例を思い出してみましょう。
- </para>
- <programlisting language="php"><![CDATA[
- <?php if ($this->books): ?>
- <!-- 本の一覧 -->
- <table>
- <tr>
- <th>著者</th>
- <th>タイトル</th>
- </tr>
- <?php foreach ($this->books as $key => $val): ?>
- <tr>
- <td><?php echo $this->escape($val['author']) ?></td>
- <td><?php echo $this->escape($val['title']) ?></td>
- </tr>
- <?php endforeach; ?>
- </table>
- <?php else: ?>
- <p>表示する本がありません。</p>
- <?php endif;?>
- ]]></programlisting>
- <sect2 id="zend.view.scripts.escaping">
- <title>出力のエスケープ</title>
- <para>
- ビュースクリプトで行うべき仕事のうち最も重要なもののひとつは、
- 出力を適切にエスケープすることです。これは、
- クロスサイトスクリプティング攻撃を防ぐのを助けます。
- それ自身がエスケープを行ってくれるような関数、メソッド、
- あるいはヘルパーを使用しているのでない限り、
- 変数を出力する際には常にそれをエスケープしなければなりません。
- </para>
- <para>
- <classname>Zend_View</classname> の escape() というメソッドが、このエスケープを行います。
- </para>
- <programlisting language="php"><![CDATA[
- // ビュースクリプトの悪い例
- echo $this->variable;
- // ビュースクリプトのよい例
- echo $this->escape($this->variable);
- ]]></programlisting>
- <para>
- デフォルトでは、escape() メソッドは <acronym>PHP</acronym> の htmlspecialchars()
- 関数でエスケープを行います。しかし環境によっては、
- 別の方法でエスケープしたくなることもあるでしょう。
- コントローラから setEscape() メソッドを実行することで、
- エスケープに使用するコールバックを <classname>Zend_View</classname> に通知できます。
- </para>
- <programlisting language="php"><![CDATA[
- // Zend_View のインスタンスを作成します
- $view = new Zend_View();
- // エスケープに htmlentities を使用するように通知します
- $view->setEscape('htmlentities');
- // あるいは、クラスの静的メソッドを使用するように通知します
- $view->setEscape(array('SomeClass', 'methodName'));
- // あるいは、インスタンスメソッドを指定することもできます
- $obj = new SomeClass();
- $view->setEscape(array($obj, 'methodName'));
- // そして、ビューをレンダリングします
- echo $view->render(...);
- ]]></programlisting>
- <para>
- コールバック関数あるいはメソッドは、
- エスケープする値を最初のパラメータとして受け取ります。
- それ以外のパラメータはオプションとなります。
- </para>
- </sect2>
- <sect2 id="zend.view.scripts.templates">
- <title>別のテンプレートシステムの使用</title>
- <para>
- <acronym>PHP</acronym> 自身も強力なテンプレートシステムではありますが、
- 開発者の多くは、デザイナにとっては高機能すぎる/複雑すぎる
- と感じており、別のテンプレートエンジンをほしがっているようです。
- <classname>Zend_View</classname> では、そのような目的のために二種類の仕組みを提供します。
- ビュースクリプトを使用することによるものと、
- <classname>Zend_View_Interface</classname> 実装することによるものです。
- </para>
- <sect3 id="zend.view.scripts.templates.scripts">
- <title>ビュースクリプトを使用したテンプレートシステム</title>
- <para>
- ビュースクリプトを使用して、PHPLIB 形式のテンプレートのような
- 別のテンプレートオブジェクトのインスタンスを作成し、
- それを操作できます。ビュースクリプトをこのように使用する方法は、
- 以下のようになります。
- </para>
- <programlisting language="php"><![CDATA[
- include_once 'template.inc';
- $tpl = new Template();
- if ($this->books) {
- $tpl->setFile(array(
- "booklist" => "booklist.tpl",
- "eachbook" => "eachbook.tpl",
- ));
- foreach ($this->books as $key => $val) {
- $tpl->set_var('author', $this->escape($val['author']);
- $tpl->set_var('title', $this->escape($val['title']);
- $tpl->parse("books", "eachbook", true);
- }
- $tpl->pparse("output", "booklist");
- } else {
- $tpl->setFile("nobooks", "nobooks.tpl")
- $tpl->pparse("output", "nobooks");
- }
- ]]></programlisting>
- <para>
- 関連するテンプレートファイルは、このようになります。
- </para>
- <programlisting language="html"><![CDATA[
- <!-- booklist.tpl -->
- <table>
- <tr>
- <th>著者</th>
- <th>タイトル</th>
- </tr>
- {books}
- </table>
- <!-- eachbook.tpl -->
- <tr>
- <td>{author}</td>
- <td>{title}</td>
- </tr>
- <!-- nobooks.tpl -->
- <p>表示する本がありません。</p>
- ]]></programlisting>
- </sect3>
- <sect3 id="zend.view.scripts.templates.interface">
- <title>Zend_View_Interface を使用したテンプレート</title>
- <para>
- <classname>Zend_View</classname> 互換のテンプレートエンジンを使用するほうが簡単だという人もいるでしょう。
- <classname>Zend_View_Interface</classname> では、
- 互換性を保つために最低限必要なインターフェイスを定義しています。
- </para>
- <programlisting language="php"><![CDATA[
- /**
- * テンプレートエンジンオブジェクトを返します
- */
- public function getEngine();
- /**
- * ビュースクリプト/テンプレートへのパスを設定します
- */
- public function setScriptPath($path);
- /**
- * すべてのビューリソースへのベースパスを設定します
- */
- public function setBasePath($path, $prefix = 'Zend_View');
- /**
- * ビューリソースへのベースパスを追加します
- */
- public function addBasePath($path, $prefix = 'Zend_View');
- /**
- * 現在のスクリプトのパスを取得します
- */
- public function getScriptPaths();
- /**
- * テンプレート変数をオブジェクトのプロパティとして代入するためのオーバーロードメソッド
- */
- public function __set($key, $value);
- public function __isset($key);
- public function __unset($key);
- /**
- * テンプレート変数を手動で代入したり、複数の変数を
- * 一括設定したりします
- */
- public function assign($spec, $value = null);
- /**
- * 代入済みのテンプレート変数を削除します
- */
- public function clearVars();
- /**
- * $name というテンプレートをレンダリングします
- */
- public function render($name);
- ]]></programlisting>
- <para>
- このインターフェイスを使用すると、
- サードパーティのテンプレートエンジンをラップして
- <classname>Zend_View</classname> 互換のクラスを作成することが簡単になります。
- 例として、Smarty 用のラッパーはこのようになります。
- </para>
- <programlisting language="php"><![CDATA[
- class Zend_View_Smarty implements Zend_View_Interface
- {
- /**
- * Smarty object
- * @var Smarty
- */
- protected $_smarty;
- /**
- * コンストラクタ
- *
- * @param string $tmplPath
- * @param array $extraParams
- * @return void
- */
- public function __construct($tmplPath = null, $extraParams = array())
- {
- $this->_smarty = new Smarty;
- if (null !== $tmplPath) {
- $this->setScriptPath($tmplPath);
- }
- foreach ($extraParams as $key => $value) {
- $this->_smarty->$key = $value;
- }
- }
- /**
- * テンプレートエンジンオブジェクトを返します
- *
- * @return Smarty
- */
- public function getEngine()
- {
- return $this->_smarty;
- }
- /**
- * テンプレートへのパスを設定します
- *
- * @param string $path パスとして設定するディレクトリ
- * @return void
- */
- public function setScriptPath($path)
- {
- if (is_readable($path)) {
- $this->_smarty->template_dir = $path;
- return;
- }
- throw new Exception('無効なパスが指定されました');
- }
- /**
- * 現在のテンプレートディレクトリを取得します
- *
- * @return string
- */
- public function getScriptPaths()
- {
- return array($this->_smarty->template_dir);
- }
- /**
- * setScriptPath へのエイリアス
- *
- * @param string $path
- * @param string $prefix Unused
- * @return void
- */
- public function setBasePath($path, $prefix = 'Zend_View')
- {
- return $this->setScriptPath($path);
- }
- /**
- * setScriptPath へのエイリアス
- *
- * @param string $path
- * @param string $prefix Unused
- * @return void
- */
- public function addBasePath($path, $prefix = 'Zend_View')
- {
- return $this->setScriptPath($path);
- }
- /**
- * 変数をテンプレートに代入します
- *
- * @param string $key 変数名
- * @param mixed $val 変数の値
- * @return void
- */
- public function __set($key, $val)
- {
- $this->_smarty->assign($key, $val);
- }
- /**
- * empty() や isset() のテストが動作するようにします
- *
- * @param string $key
- * @return boolean
- */
- public function __isset($key)
- {
- return (null !== $this->_smarty->get_template_vars($key));
- }
- /**
- * オブジェクトのプロパティに対して unset() が動作するようにします
- *
- * @param string $key
- * @return void
- */
- public function __unset($key)
- {
- $this->_smarty->clear_assign($key);
- }
- /**
- * 変数をテンプレートに代入します
- *
- * 指定したキーを指定した値に設定します。あるいは、
- * キー => 値 形式の配列で一括設定します
- *
- * @see __set()
- * @param string|array $spec 使用する代入方式 (キー、あるいは キー => 値 の配列)
- * @param mixed $value (オプション) 名前を指定して代入する場合は、ここで値を指定します
- * @return void
- */
- public function assign($spec, $value = null)
- {
- if (is_array($spec)) {
- $this->_smarty->assign($spec);
- return;
- }
- $this->_smarty->assign($spec, $value);
- }
- /**
- * 代入済みのすべての変数を削除します
- *
- * Zend_View に {@link assign()} やプロパティ
- * ({@link __get()}/{@link __set()}) で代入された変数をすべて削除します
- *
- * @return void
- */
- public function clearVars()
- {
- $this->_smarty->clear_all_assign();
- }
- /**
- * テンプレートを処理し、結果を出力します
- *
- * @param string $name 処理するテンプレート
- * @return string 出力結果
- */
- public function render($name)
- {
- return $this->_smarty->fetch($name);
- }
- }
- ]]></programlisting>
- <para>
- この例では、<classname>Zend_View</classname> ではなく
- <classname>Zend_View_Smarty</classname> クラスのインスタンスを作成し、
- それを使用して <classname>Zend_View</classname> と同じようなことをしています。
- </para>
- <programlisting language="php"><![CDATA[
- //例 1. InitializerのinitView()で
- $view = new Zend_View_Smarty('/path/to/templates');
- $viewRenderer =
- Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
- $viewRenderer->setView($view)
- ->setViewBasePathSpec($view->_smarty->template_dir)
- ->setViewScriptPathSpec(':controller/:action.:suffix')
- ->setViewScriptPathNoControllerSpec(':action.:suffix')
- ->setViewSuffix('tpl');
- //例 2. アクションコントローラでも同様に...
- class FooController extends Zend_Controller_Action
- {
- public function barAction()
- {
- $this->view->book = 'Zend PHP 5 Certification Study Guide';
- $this->view->author = 'Davey Shafik and Ben Ramsey'
- }
- }
- //例 3. アクションコントローラでのビューの初期化
- class FooController extends Zend_Controller_Action
- {
- public function init()
- {
- $this->view = new Zend_View_Smarty('/path/to/templates');
- $viewRenderer = $this->_helper->getHelper('viewRenderer');
- $viewRenderer->setView($this->view)
- ->setViewBasePathSpec($view->_smarty->template_dir)
- ->setViewScriptPathSpec(':controller/:action.:suffix')
- ->setViewScriptPathNoControllerSpec(':action.:suffix')
- ->setViewSuffix('tpl');
- }
- }
- ]]></programlisting>
- </sect3>
- </sect2>
- </sect1>
- <!--
- vim:se ts=4 sw=4 et:
- -->
|