Smartyの説明は必要ですか?
まぁ、いいか…
本稿ではZendFrameworkでSmartyというテンプレートエンジンを利用する方法を記述します。
Smartyのインストール
Smartyも言ってしまえばPHPなので、別段難しいことはありません。適当なフォルダに配置して、include_pathに通せばとりあえずOKです。
安定を求めないワタシは、何事も最新がいいということで、Smarty提供のレポジトリから最新のリビジョンをチェックアウトすることにします。Smartyには2.x系と3.x系があるのですが(2011/02/09 現在)、もちろん3.x系を選んでいます。
Zendの時と同様に$HOME/local/lib/php/Smartyに配置します。
で、ホームディレクトリで下記コマンドを実行すればOK
[bash]
$ svn checkout http://smarty-php.googlecode.com/svn/trunk/distribution/libs local/lib/php/Smarty
[/bash]
シェルに書いておくと便利ですね。ちなみに私はこれをupdate_smarty.shとしてホームディレクトリに置いてあります。
最新のソースに更新する場合は以下のコマンドを実行します。
[bash]
$ svn update local/lib/php/Smarty
[/bash]
バグがFIXされたら、これで最新のソースに更新しましょう。
Zend_View_Interfaceを用いたSmartyビュークラス
こちらのページにご丁寧にSmartyを使用する場合のZend_Viewの利用方法が書いてあるので、参考にします。
どうやらこんなクラスを作ればいいようです。
ほぼ丸写しですが、コンストラクタだけちょびっと変えてあります。こうした方が、初期化するときにいろいろとキレイに書けそうなので…
[php]
require_once(‘Smarty/Smarty.class.php’);
class Zend_View_Smarty implements Zend_View_Interface {
/**
* Smarty object
* @var Smarty
*/
protected $_smarty;
/**
* コンストラクタ
*
* @param array $_wa_param Smarty初期化パラメータ
* @return void
*/
public function __construct($_wa_param=array()) {
$this->_smarty = new Smarty;
foreach ($_wa_param as $key => $value) {
if ($key == ‘template_dir’) {
$this->setScriptPath($value);
} else {
$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);
}
}
[/php]
ワタシはこれをapplication/lib配下に作っています。
Smartyの初期化とレンダーの設定
Controllerクラスのinit()メソッド内でレンダーにSmartyの設定を行います。
[php]
public function init() {
// Smarty設定
$_wa_param = array(
‘template_dir’ => APPLICATION_PATH.’/views/templates’,
‘compile_dir’ => APPLICATION_PATH.’/views/templates_c’
);
$this->view = new Zend_View_Smarty($_wa_param);
$viewRenderer = $this->_helper->getHelper(‘viewRenderer’);
$viewRenderer->setView($this->view)
->setViewBasePathSpec($this->view->getEngine()->template_dir)
->setViewScriptPathSpec(‘:controller/:action.:suffix’)
->setViewScriptPathNoControllerSpec(‘:action.:suffix’)
->setViewSuffix(‘tpl’);
}
[/php]
各コントローラで記述する必要があるので、抽象クラスなどをつくる等して、共通化しませう。
無論、Zend_View_Smartyファイルをrequireしておく必要ありです。
[php]
require_once ‘Zend_View_Smarty.php’;
[/php]
使ってみる
コントローラのアクション内で
[php]
$this->view->hoge = date(‘YmdHis’);
//または
$this->view->assign(‘hoge’, date(‘YmdHis’));
[/php]
と書いて、テンプレート内で
[xml]
<html>
<body>
{$hoge}
</body>
</html>
[/xml]
とすればOK!

