忍者ブログ
[PR]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。



2024/04/26 07:48 |
ZF1.11:Zend_ControllerでSmarty
Zend_Controller標準のビューはZend_Viewですが、機能は貧弱です。
というかサンプルのテンプレートを見ればわかりますが、ほとんど素のPHPです。
ヘルパーはかなり用意されていますが、使い方を見てもどうも全然便利そうに見えない不思議。

というわけでSmartyに差し替えましょう。
幸い公式自らSmarty用ラッパを公開してくれています。

まずはSmartyの準備。
applicationディレクトリ内にSmarty用ディレクトリを作成。
application/
    ├templates/
    └smarty/
        ├libs/
        ├cache/
        ├configs/
        └templates_c/


templatesフォルダにテンプレートを置き、それ以外の本体やキャッシュなんかはsmartyフォルダに突っ込むことにします。
Smarty公式サイトからダウンロードして解凍、中身のlibsフォルダをapplication/smarty/libsに設置。

次にZend_View_Smartyクラスをさくっとコピーして、Zend/View/Smarty.phpに設置します。
これはZend_ViewからSmartyを呼び出せるように連携させるクラスです。

ダミーのindex.htmlを設置。

application/templates/index/index.html
	<html><body>Hello, World!{$smarty.now}</body></html>
最後にZend_Viewを呼び出すかわりにZend_View_Smartyを呼び出すように設定変更します。

html/index.php
<?php
	//define
	define('APP_DIR', dirname(__FILE__).'/../application/');
		define('SMARTY_DIR', APP_DIR.'smarty/libs/');
		define('SMARTY_TEMPLATE_DIR', APP_DIR.'templates/');
		define('SMARTY_COMPLIE_DIR', APP_DIR.'smarty/templates_c/');
		define('SMARTY_CACHE_DIR', APP_DIR.'smarty/cache/');
		define('SMARTY_CONFIG_DIR', APP_DIR.'smarty/config/');
	
	//require
	require_once('Zend/Controller/Front.php');
	require_once('Zend/View/Smarty.php');
	require_once(APP_DIR.'smarty/libs/Smarty.class.php');
	
	//Zend_View_Smarty
	$view = new Zend_View_Smarty(SMARTY_TEMPLATE_DIR, array('compile_dir'=>SMARTY_COMPLIE_DIR, 'cache_dir'=>SMARTY_CACHE_DIR, 'config_dir'=>SMARTY_CONFIG_DIR));
	//Zend_Controller_Action_Helper_ViewRenderer
	$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
	$viewRenderer->setView($view)
	             ->setViewBasePathSpec($view->_smarty->template_dir)
	             ->setViewScriptPathSpec(':controller/:action.:suffix')
	             ->setViewScriptPathNoControllerSpec(':action.:suffix')
	             ->setViewSuffix('html');

	//実行
	Zend_Controller_Front::run(APP_DIR.'controllers');
実行。

Fatal error: Cannot access protected property Zend_View_Smarty::$_smarty in C:\hoge\fuga\html\index.php on line 21

あら?

具体的に死んでる行はここ。
setViewBasePathSpec($view->_smarty->template_dir)

なんでかというとZend_View_Smartyでprotected $_smarty;ってやってるせい。

http://helog.jp/php/zend-framework/984/とかPHP5.3.0なのにどうして動いてるんだろう?ふしぎ。

今回はテンプレートディレクトリをSMARTY_TEMPLATE_DIRで定義してるので素直にsetViewBasePathSpec(SMARTY_TEMPLATE_DIR)と書き直して実行。

d7c81437.PNG

見事にSmartyの適用に成功しました。

setViewScriptPathSpecとかsetViewScriptPathNoControllerSpecとかはリクエストパラメータの処理方法みたいです。
よくわからないのでとりあえずそのままで。
setViewSuffixはテンプレートの拡張子です。
デフォルトは'phtml'とかいう変な拡張子になっているので'html'に変更します。
以上で、Zend_ControllerでSmartyを利用することができるようになりました。
PR


2011/11/25 21:42 | Comments(0) | TrackBack() | PHP

トラックバック

トラックバックURL:

コメント

コメントを投稿する






Vodafone絵文字 i-mode絵文字 Ezweb絵文字 (絵文字)



<<Minecraft 1.8.1 IndustrialCraft2 採掘用ドリル、チェーンソーを作る | HOME | Minecraft 1.8.1 IndustrialCraft2 電気炉、抽出機、圧縮機、電圧計を作る>>
忍者ブログ[PR]