display
void
display ( string template [, string cache_id [, string compile_id]])
テンプレートを表示します。第1パラメータには、有効なテンプレートリソースの種類を含んだパスを指定する事ができます。任意の第2パラメータにはキャッシュIDを渡す事ができます。詳細は、キャッシュの項を参照して下さい。
任意の第3パラメータとして compile_id を渡す事が出来ます。
異なる言語でコンパイルされた別々のテンプレートが存在するような、同じテンプレートの異なるバージョンをコンパイルしたい場合に利用します。
他にも、1つ以上の $template_dir を持っているが $compile_dir が1つしかない場合です。
それぞれの $template_dir のために別々の compile_id をセットしなければ、同名のテンプレートはお互いに上書きされてしまいます。
この関数をコールする度に compile_id をセットする代わりに、一度 $compile_id 変数をセットする事が出来ます。
例 1. display
<?php include("Smarty.class.php"); $smarty = new Smarty; $smarty->caching = true;
// キャッシュが存在しない場合はデータベースを呼び出す if(!$smarty->is_cached("index.tpl")) {
// ダミーデータ $address = "245 N 50th"; $db_data = array( "City" => "Lincoln", "State" => "Nebraska", "Zip" => "68502" );
$smarty->assign("Name","Fred"); $smarty->assign("Address",$address); $smarty->assign($db_data);
}
// 出力を表示 $smarty->display("index.tpl"); ?>
|
|
$template_dirディレクトリ外のファイルを表示するためには、テンプレートリソースを指定します。
例 2. display関数にテンプレートリソースを指定した例
<?php // ファイルの絶対パス $smarty->display("/usr/local/include/templates/header.tpl");
// ファイルの絶対パス (上と同じ) $smarty->display("file:/usr/local/include/templates/header.tpl");
// windows環境の絶対パス (接頭辞に"file:"を使う必要がある) $smarty->display("file:C:/www/pub/templates/header.tpl");
// "db"と名付けられたテンプレートリソースからインクルードする $smarty->display("db:header.tpl"); ?>
|
|