Zend FrameworkのZend_ViewをSmartyに

| | コメント(0) | トラックバック(0)
はじめは別にZend_Viewで十分かな、と思ったけど、やっぱ<?= ~ ?>が乱れ咲くViewが耐えられなくなった。それにキャッシュ機能も魅力的に感じたのでSmartyを導入してみることにした。
ちなみに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)抜粋
    /**
     * コンストラクタ
     *
     * @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;
//        }

コンフィグファイル(application.ini)抜粋
[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

こんな感じかなー。もっといいやり方あったら教えてつかあさい。


トラックバック(0)

このブログ記事を参照しているブログ一覧: Zend FrameworkのZend_ViewをSmartyに

このブログ記事に対するトラックバックURL: http://hirop0164.s326.xrea.com/mt/mt-tb.cgi/220

コメントする


画像の中に見える文字を入力してください。

このブログ記事について

このページは、ぴろしが2008年12月 5日 23:30に書いたブログ記事です。

ひとつ前のブログ記事は「AMI(Amazon Machine Image)の保存方法のメモ」です。

次のブログ記事は「Amazon EC2で動いているCentOSにSLコマンドを入れる」です。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。