はじめは別にZend_Viewで十分かな、と思ったけど、やっぱ<?= ~ ?>が乱れ咲くViewが耐えられなくなった。それにキャッシュ機能も魅力的に感じたのでSmartyを導入してみることにした。
ちなみにSmartyとかJavaのVelocityとかの紹介でよくある「デザインと設計を分けるための仕組み」的な紹介文言は寝言だと思ってます。これ、具体的根拠となる事例が併記されているのみたことないんだけど、書いている人は考えて書いてるのかな。
それとも俺がおかしい?まあいいや。
ちなみにSmartyとかJavaのVelocityとかの紹介でよくある「デザインと設計を分けるための仕組み」的な紹介文言は寝言だと思ってます。これ、具体的根拠となる事例が併記されているのみたことないんだけど、書いている人は考えて書いてるのかな。
それとも俺がおかしい?まあいいや。
Zend_Viewとの連携方法はいろんなとこで書いてあるから割愛。
キャッシュ用ディレクトリとかコンパイルチェックとかの設定は開発環境とか本番環境だと違ってくるから設定ファイルとかで管理したい。ので、その辺のメモ。
フロントコントローラ(index.php)抜粋
define(SITE_ENVIRONMENT, 'develop');
$view = new Xxx_View_Smarty();
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
$viewRenderer->setViewBasePathSpec(dirname(__FILE__) . '/views/templates')
->setViewScriptPathSpec(':controller/:action.:suffix')
->setViewScriptPathNoControllerSpec(':action.:suffix')
->setViewSuffix('tpl');
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
Smarty用のラッパー(Xxx_View_Smarty)抜粋
/**コンフィグファイル(application.ini)抜粋
* コンストラクタ
*
* @param string $tmplPath
* @param array $extraParams
* @return void
*/
public function __construct()
{
$config = new Zend_Config_Ini(
dirname(__FILE__). '/../../../config/application.ini', SITE_ENVIRONMENT);
$this->_smarty = new Smarty;
$this->_smarty->compile_dir = $config->smarty->compile_dir;
$this->_smarty->cache_dir = $config->smarty->cache_dir;
$this->_smarty->caching = $config->smarty->caching;
$this->_smarty->compile_check = $config->smarty->compile_check;
$this->_smarty->cache_lifetime = $config->smarty->cache_lifetime;
if (null !== $tmplPath) {
$this->setScriptPath($tmplPath);
}
// foreach ($extraParams as $key => $value) {
// $this->_smarty->$key = $value;
// }
[develop]こんな感じかなー。もっといいやり方あったら教えてつかあさい。
smarty.compile_dir = C:\Dev\php\compile_dir
smarty.cache_dir = C:\Dev\php\cache_dir
smarty.caching = 1
smarty.compile_check = true
smarty.cache_lifetime = 1
[product]
smarty.compile_dir = /tmp/compile_dir
smarty.cache_dir = /tmp/cache_dir
smarty.caching = 1
smarty.compile_check = false
smarty.cache_lifetime = 5
コメントする