「Smarty」の編集履歴(バックアップ)一覧に戻る
Smarty - (2005/08/09 (火) 11:26:39) のソース
Smartyに関するメモ。 #contents *インストール (ApacheとPHPが既にインストールされているという前提) Smarty本体を http://smarty.php.net/ からダウンロード。 ダウンロードしたファイル(Smarty-x.x.x.tar.gz)を解凍すると以下のファイルができあがる。 必要なのはlibs以下のファイルのみ。 [ディレクトリ] demo libs←これのみ必要 misc unit_test [ファイル] COPYING.lib BUGS ChangeLog FAQ INSTALL NEWS QUICK_START README RELEASE_NOTES TODO PHPインストールフォルダ以下にSmartyを置くフォルダを作成。(ここでは c:\php\lib\Smarty とする) 解凍したlibs以下のファイル・フォルダ全てをc:\php\lib\Smarty以下にコピーする。 この時点でのフォルダ構成は以下。 c:\php\lib\Smarty ∟ internals(ディレクトリ) plugins(ディレクトリ) Config_File.class.php Smarty.class.php Smarty_Compiler.class.php debug.tpl php.iniファイルに以下のパスを記述して上書き保存。 include_path = ".;c:\php\lib\Smarty" Apacheを再起動して完了。 *Smarty 動くかテスト Apacheのドキュメントルート以下にSmartyテスト用フォルダを作成。(ここでは SmartyTest とする) SmartyTestフォルダに以下のフォルダとファイルを作成。 [フォルダ構成] htdocs\SmartyTest ∟index.php cache(ディレクトリ) config(ディレクトリ) templates_c(ディレクトリ) templates(ディレクトリ) ∟sample.tpl 注)上記4つのディレクトリはApacheのドキュメントルート以外に置いたほうがよい [index.phpの内容] <?php //Smartyライブラリ読み込み require_once("Smarty.class.php"); $smarty = new Smarty; $smarty->assign('title','タイトル'); $smarty->assign('name', 'お名前'); $smarty->display('sample.tpl'); ?> [sample.tplの内容] <html> <head> <title>{$title}</title> </head> <body> 名前:{$name} </body> </html> http://localhost/SmartyTest/index.php にアクセス。エラー出なければOK。 * Smarty セットアップ用のクラスを作成 Smartyを使うときに毎回「require_once("Smarty.class.php");」やら「$smarty->template_dir = "xxxx";」やら書くのは効率が悪いので、専用のSmarty設定クラスを作成する。 [SmartySetup.php] <?php require_once("Smarty.class.php"); class SmartySetup extends Smarty { public function __construct() { $this->Smarty(); //Smartyで用いるディレクトリの設定 $this->template_dir = "c:/xxxx/templates/"; $this->compile_dir = "c:/xxxx/templates_c/"; $this->config_dir = "c:/xxxx/config/"; $this->cache_dir = "c:/xxxx/cache/"; } } ?> 使うときは以下のようにする。ちょっとだけ書くのが楽になった。 [index.php] <?php require_once("SmartySetup.php"); $smarty = new SmartySetup; $smarty->display("index.tpl"); ?> * Shift_JISでテンプレート作成時に文字化け sjisでテンプレートファイルを作成し、「施設」という文字を埋め込んだらエラーになった。回避策は{literal}で囲う。sjisで作らないことが一番の回避策? {literal}施設{/literal} Smartyのプリフィルタとポストフィルタを使えばいちいち{literal}で囲わなくてもOK。 function pre01($buff, &$smarty) { return mb_convert_encoding($buff,"EUC-JP","SJIS"); } function post01($buff, &$smarty) { return mb_convert_encoding($buff,"SJIS","EUC-JP"); } $smarty = new Smarty; $smarty->register_prefilter('pre01'); $smarty->register_postfilter('post01'); $smarty->display('index.tpl'); *{html_options}でlabel属性がついてしまう label属性を削除するには、smartyフォルダ/plugins/function.html_options.phpの99行目を以下のように修正。 $_html_result = '<option value="' . *smartyの関数メモ URLエンコード {$text|escape:'url'}