標準のZend Viewより使い慣れたSmartyを使いたかったので差し替えてみることに。
Zend Framework: Document - 35.3.2. 別のテンプレートシステムの使用の項目だけだとSmartyは呼び出せても実際どうやってControllerに組み込むの?となってしまう。
とりあえずControllerのViewを差し替えてみるかと思いZend_Controller_Actionを継承してinit()でSmartyを差し替えた。
<?php
class Sample_Controller_Action extends Zend_Controller_Action {
/**
* @var Zend_Config
*/
protected $_config;
public function init() {
// コンフィグの読み込み
$this->_config = Zend_Registry::get('config');
$extraParams = array(
'compile_dir' => $config->smarty->compile_dir,
);
$this->view = new Sample_View_Smarty(null, $extraParams);
$this->viewSuffix = 'tpl';
}
}
が、上記だと上手くいかなかったわけで。
続いてActionHelperのViewRendererとしてSmartyを登録する方法で挑戦。
$config->smarty->compile_dir,
);
// マニュアルに書かれているSmarty用View
$view = new Sample_View_Smarty(null, $extraParams);
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
$viewRenderer->setViewBasePathSpec($config->smarty->template_dir)
->setViewScriptPathSpec(':controller/:action.:suffix')
->setViewScriptPathNoControllerSpec(':action.:suffix')
->setViewSuffix('tpl');
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
上記方法で変更出来た。
これは継承したinit()では書かず、Controllerが呼ばれる前に書いた(Zend_Controller_Front::run()の前)。複数のテンプレートエンジン使い分ける事はほとんど無いと思われるのでこれで大丈夫かなと。
