Smarty - (2005/08/02 (火) 14:41:07) の編集履歴(バックアップ)
Smartyに関するメモ。
インストール
(ApacheとPHPが既にインストールされているという前提)
Smarty本体を http://smarty.php.net/ からダウンロード。
ダウンロードしたファイル(Smarty-x.x.x.tar.gz)を解凍すると以下のファイルができあがる。
必要なのはlibs以下のファイルのみ。
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以下にコピーする。
この時点でのフォルダ構成は以下。
解凍した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フォルダに以下のフォルダとファイルを作成。
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"); ?>
危険文字「施」?
sjisでテンプレートファイルを作成し、「施設」という文字を埋め込んだらエラーになった。回避策は以下。sjisで作らないことが一番の回避策?
[回避策] {literal}施設{/literal} という具合に{literal}で囲う。
確認した危険文字リスト ・施 ・本 ・須
smartyの関数メモ
URLエンコード
{$text|escape:'url'}