「Smarty」(2005/12/13 (火) 11:06:39) の最新版変更点
追加された行は緑色になります。
削除された行は赤色になります。
#contents
*インストール
(ApacheとPHPが既にインストールされているという前提)
Smarty本体を http://smarty.php.net/ からダウンロード。ダウンロードしたファイル(Smarty-x.x.x.tar.gz)を解凍すると以下のファイルができあがる。
[ディレクトリ]
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フォルダに以下のフォルダとファイルを作成。
注)本来はセキュリティを考慮して下記4つのディレクトリはApacheのドキュメントルート以外に置いたほうがよい。もし置くなら、.htaccessファイルなどでwebから直接参照できないようにしておいたほうが吉。
[フォルダ構成]
htdocs\SmartyTest
∟index.php
cache(ディレクトリ)
config(ディレクトリ)
templates_c(ディレクトリ)
templates(ディレクトリ)
∟sample.tpl
[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");
?>
*Smarty セットアップ用のクラスを作成その2
[SmartySetup.php]
<?php
require_once("Smarty.class.php");
class SmartySetup extends Smarty
{
public function __construct($smarty_data = array(), $tpl_name)
{
$this->Smarty();
// 設定
$this->template_dir = "smarty/template";
$this->compile_dir = "smarty/template_c";
$this->config_dir = "smarty/config";
$this->cache_dir = "smarty/cache";
$this->caching = 0; // キャッシュ無効 1で有効
// テンプレート文字化け対策用プリフィルタとポストフィルタ
$this->register_prefilter(array($this,'pre1'));
$this->register_postfilter(array($this,'post1'));
// データをアサインしてテンプレートを表示
$this->assign($smarty_data);
$this->display($tpl_name);
// アサインしたデータを破棄
$this->clear_all_assign();
}
// プリフィルタ
public function pre1($tpl_source, &$smarty)
{
return mb_convert_encoding($tpl_source,"EUC-JP","SJIS");
}
// ポストフィルタ
public function post1($tpl_source, &$smarty)
{
return mb_convert_encoding($tpl_source,"SJIS","EUC-JP");
}
}
?>
使うときは以下のようなかんじ
[index.php]
<?php
require_once("SmartySetup.php");
$data = array(
"data1"=>"ほげー"
,"data2"=>"ほげほげー"
);
$tpl = "index.tpl";
$smarty = new SmartySetup($data, $tpl);
?>
[index.tpl]
<html>
<head><title>index</title></head>
<body>
データその1:{$data1}<br>
データその2:{$data2}<br>
</body>
</html>
index.phpにアクセスして以下のように表示されるはず(未検証)
データその1:ほげー
データその2:ほげほげー
*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'}
スペースやタブを削除
{strip}{/strip}で囲う
デバッグ
{debug}
(別ウインドウでデバッグウインドウが立ち上がる)
中括弧を表示させる
右側中括弧
{rdelim}
左側中括弧
{ldelim}
#contents
*インストール
(ApacheとPHPが既にインストールされているという前提)
Smarty本体を http://smarty.php.net/ からダウンロード。ダウンロードしたファイル(Smarty-x.x.x.tar.gz)を解凍すると以下のファイルができあがる。
[ディレクトリ]
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フォルダに以下のフォルダとファイルを作成。
注)本来はセキュリティを考慮して下記4つのディレクトリはApacheのドキュメントルート以外に置いたほうがよい。もし置くなら、.htaccessファイルなどでwebから直接参照できないようにしておいたほうが吉。
[フォルダ構成]
htdocs\SmartyTest
∟index.php
cache(ディレクトリ)
config(ディレクトリ)
templates_c(ディレクトリ)
templates(ディレクトリ)
∟sample.tpl
[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");
?>
*Smarty セットアップ用のクラスを作成その2
[SmartySetup.php]
<?php
require_once("Smarty.class.php");
class SmartySetup extends Smarty
{
public function __construct($smarty_data = array(), $tpl_name)
{
$this->Smarty();
// 設定
$this->template_dir = "smarty/template";
$this->compile_dir = "smarty/template_c";
$this->config_dir = "smarty/config";
$this->cache_dir = "smarty/cache";
$this->caching = 0; // キャッシュ無効 1で有効
// テンプレート文字化け対策用プリフィルタとポストフィルタ
$this->register_prefilter(array($this,'pre1'));
$this->register_postfilter(array($this,'post1'));
// データをアサインしてテンプレートを表示
$this->assign($smarty_data);
$this->display($tpl_name);
// アサインしたデータを破棄
$this->clear_all_assign();
}
// プリフィルタ
public function pre1($tpl_source, &$smarty)
{
return mb_convert_encoding($tpl_source,"EUC-JP","SJIS");
}
// ポストフィルタ
public function post1($tpl_source, &$smarty)
{
return mb_convert_encoding($tpl_source,"SJIS","EUC-JP");
}
}
?>
使うときは以下のようなかんじ
[index.php]
<?php
require_once("SmartySetup.php");
$data = array(
"data1"=>"ほげー"
,"data2"=>"ほげほげー"
);
$tpl = "index.tpl";
$smarty = new SmartySetup($data, $tpl);
?>
[index.tpl]
<html>
<head><title>index</title></head>
<body>
データその1:{$data1}<br>
データその2:{$data2}<br>
</body>
</html>
index.phpにアクセスして以下のように表示されるはず(未検証)
データその1:ほげー
データその2:ほげほげー
*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'}
スペースやタブを削除
{strip}{/strip}で囲う
デバッグ
{debug}
(別ウインドウでデバッグウインドウが立ち上がる)
中括弧を表示させる
右側中括弧
{rdelim}
左側中括弧
{ldelim}
*サーバにアップしたら画面が表示されないエラー
PHP+smartyを用いてローカル環境で開発していたものをサーバにそのままアップするとエラーが出て画面が表示されない場合がある。サーバにアップした際に以下の操作をしておけば大体大丈夫?
-smartyコンパイルディレクトリ内ファイル(template_c以下)を全部削除
-smartyキャッシュディレクトリ内ファイル(cache以下)を全部削除
-smartyのtemplate_c、cache、configディレクトリの権限を777
表示オプション
横に並べて表示:
変化行の前後のみ表示: