Zend Framework:テンプレートエンジンをSmartyにする
Zend_Controllerを使ったWebアプリケーションのテンプレートエンジンにSmartyを使用してみます。
「Zend Framework:Hello Worldを表示してみる」のように"Hello World"を表示します。
ディレクトリ構成
ディレクトリ構成は、だいたい次のようにしています。
BASEDIR/
html/
.htaccess
index.php
lib
ZendFramework
ZendFramework-1.0.1/
webapp/
modules/
default/
controllers/
templates/
temporary
cache
templates_c
BASEDIRは、ドキュメントルートの一個上のディレクトリです。
htmlは、ドキュメントルートです。
lib/ZendFrameworkは、lib/ZendFramework-1.0.1へのシンボリックリンクにしています。
lib/smartyは、lib/Smarty-2.6.18/libsへのシンボリックリンクにしています。
Smartyのインストール
Smartyは、Smarty : Template Engineからダウンロードできます。
例:
$ cd BASEDIR/lib
$ tar xvzf Smarty-2.6.18.tar.gz
$ ln -s Smarty-2.6.18/libs smarty
escape修飾子
サンプルのテンプレートファイルでは、escape修飾子の引数に何も指定していません。
僕は、smartyに付属のmodifier.escape.phpファイルを次のように、$char_set引数のデフォルト値をUTF-8に変更しています。
Smarty-2.6.18/libs/plugins/modifier.escape.php
-
function smarty_modifier_escape($string, $esc_type = 'html', $char_set = 'UTF-8')
-
{
-
// 略
-
}
ディレクトリに書き込み権限を追加
次のディレクトリにapacheが書き込みできるようにします。
- BASEDIR/webapp/temporary/cache
- BASEDIR/webapp/temporary/templates_c
例:
$ chmod 777 BASEDIR/webapp/temporary/cache
$ chmod 777 BASEDIR/webapp/temporary/templates_c
Smarty用のViewクラス
Zend_View_Interfaceを継承して、Zend_View互換のSmartyテンプレートエンジンを使用するViewクラスを作成します。
Zend_View_Interface を使用したテンプレートにあるサンプルを元にします。
オリジナルのままではエラーになるので、少し変更しています。
私の間違いです。エラーにはなりませんので、訂正いたします(2007/11/16)。
BASEDIR/webapp04/lib/Tz/View/Smarty.php
-
class Tz_View_Smarty implements Zend_View_Interface
-
{
-
protected $_smarty;
-
-
{
-
$this->_smarty = new Smarty;
-
-
if (null !== $tmplPath) {
-
$this->setScriptPath($tmplPath);
-
}
-
-
foreach ($extraParams as $key => $value) {
-
$this->_smarty->$key = $value;
-
}
-
}
-
-
public function getEngine()
-
{
-
return $this->_smarty;
-
}
-
-
public function setScriptPath($path)
-
{
-
$this->_smarty->template_dir = $path;
-
return;
-
}
-
throw new Exception('無効なパスが指定されました');
-
}
-
-
public function getScriptPath()
-
{
-
return $this->_smarty->template_dir;
-
}
-
-
public function __set($key, $val)
-
{
-
$this->_smarty->assign($key, $val);
-
}
-
-
public function __get($key)
-
{
-
return $this->_smarty->get_template_vars($key);
-
}
-
-
public function __isset($key)
-
{
-
return (null !== $this->_smarty->get_template_vars($key));
-
}
-
-
public function __unset($key)
-
{
-
$this->_smarty->clear_assign($key);
-
}
-
-
public function assign($spec, $value = null)
-
{
-
$this->_smarty->assign($spec);
-
return;
-
}
-
-
$this->_smarty->assign($spec, $value);
-
}
-
-
public function assignByRef(&$spec, &$value = null)
-
{
-
$this->_smarty->assign_by_ref($spec);
-
return;
-
}
-
-
$this->_smarty->assign_by_ref($spec, $value);
-
}
-
-
public function clearVars()
-
{
-
$this->_smarty->clear_all_assign();
-
}
-
-
public function render($name)
-
{
-
return $this->_smarty->fetch($name);
-
}
-
-
public function getScriptPaths()
-
{
-
}
-
-
public function setBasePath($path, $classPrefix = 'Zend_View')
-
{
-
-
}
-
-
public function addBasePath($string, $classPrefix = 'Zend_View')
-
{
-
-
}
-
}
ViewRendererアクションヘルパーの初期化
ViewRendererを利用して、自動的にテンプレートファイルを選択して、レンダリングするようにします。
ViewRendererアクションヘルパーに、Smartyテンプレートエンジンを使用するViewオブジェクトを登録します。
また、ViewRendererアクションヘルパーのsetViewSuffixメソッドでテンプレートファイルの拡張子がhtmlになるように指定しています。
BASEDIR/webapp04/bootstrap.php
-
function setup_view_smarty($registry)
-
{
-
$webapp_dir = $registry['webappDir'];
-
$config = $registry['config'];
-
require_once $webapp_dir . '/lib/Tz/View/Smarty.php';
-
$smarty_config = $config->smarty->toArray();
-
$smarty_config['cache_dir'] = $webapp_dir . '/temporary/cache/';
-
$smarty_config['compile_dir'] = $webapp_dir . '/temporary/templates_c/';
-
}
-
}
-
$default_tpl_dir = null;// テンプレートディレクトリの指定、Tz_Controller_Action::init()で行う
-
$view = new Tz_View_Smarty($default_tpl_dir,$smarty_config);
-
-
// ViewRendererヘルパーを使用する
-
$vr = new Zend_Controller_Action_Helper_ViewRenderer();
-
$vr->setView($view);
-
$vr->setViewSuffix('html');
-
Zend_Controller_Action_HelperBroker::addHelper($vr);
-
}
テンプレートファイルの配置場所
テンプレートファイルは、コントローラディレクトリを配置したディレクトリ内のtemplatesディレクトリ以下に置くことにします。
このディレクトリがsmartyのテンプレートディレクトリとなるようにします。
例えば、BASEDIR/webapp/modules/default/templatesがテンプレートディレクトリになります。
テンプレートディレクトリは、アクションコントローラのinit()メソッドが起動されたときに設定することにします。
BASEDIR/webapp04/lib/Tz/Controller/Action.php
-
//
-
$request = $this->getRequest();
-
-
$module = $request->getModuleName();
-
$controller = $request->getControllerName();
-
$action = $request->getActionName();
-
-
$dirs = $this->getFrontController()->getControllerDirectory();
-
$module = 'default';
-
}
-
throw new Exception('テンプレートディレクトリがありません。');
-
}
-
$this->view->setScriptPath($templates_dir);
-
-
$smarty = $this->view->getEngine();
-
$smarty->compile_id = $module . '_' . $action . '_' . $controller;
テンプレートファイルの命名規則
テンプレートファイルは、テンプレートディレクトリ内に':controller/:action.:suffix'という規則で配置します。
:controllerはコントローラ名、:actionはアクション名です。
:suffixは、上記「ViewRendererアクションヘルパーの初期化」でhtmlにしています。
例えば、defaultモジュールのindexコントローラのindexアクションが使用するテンプレートファイルは、次の場所になります。
BASEDIR/webapp04/modules/default/templates/index/index.html
dbErrorのようにアクション名に大文字が含まれる場合も、テンプレートファイル名は、すべて小文字となります。
サンプルダウンロード
ダウンロードページにアーカイブしたファイルを置きました。
トラックバック URL :
こんにちは。ゼンドジャパンの吉田と申します。
内容を拝見していましたら、Smarty用のViewクラスの項目で、「オリジナルのままではエラーになるので、少し変更しています」とありました。
これは getScriptPaths()の返り値がInterfaceではarrayと定義されているのにプログラマ向けリファレンスガイドではstringを返すようになっていることをおっしゃられていますでしょうか?
もし覚えていらっしゃいましたらお返事いただけますと幸いです。
Comment by Satoru Yoshida — 2007/11/16 金曜日 @ 19:38:17
はじめまして。
> これは getScriptPaths()の返り値がInterfaceではarrayと定義されているのにプログラマ向けリファレンスガイドではstringを返すようになっていることをおっしゃられていますでしょうか?
はい。その通りです。
エラーという表現は、間違いでした。
先ほど試しましたところ、Warningでした。
Warning: Invalid argument supplied for foreach() in
(略)Zend/Controller/Action/Helper/ViewRenderer.php on line 322
失礼いたしました。
訂正いたします。
Comment by trek — 2007/11/16 金曜日 @ 20:20:45
お返事ありがとうございます。
ViewRendererでのWaringが出ている旨英文でレポートしておきます
Comment by Satoru Yoshida — 2007/11/16 金曜日 @ 21:08:07
[PHP][Zend Framework]Zend_Gdat…
Zend_GdataでYoutubeの検索を作ってみた。 わずかなコードで作れてしまった。 ※ほとんどオフィシャルのドキュメントに書いてあることだけど・・。 Zend (more…)
トラックバック by Heavens hell — 2007/12/6 木曜日 @ 1:15:32