display() — テンプレートを表示します。
void display(string template,
string cache_id,
string compile_id);
テンプレートを表示します
(fetch() と違い出力を行います)。
第1パラメータには、有効な テンプレートリソース
の種類を含むパスを指定する事ができます。任意の第2パラメータには
キャッシュID を渡す事ができます。
詳細は キャッシュの項 を参照してください。
任意の第3パラメータとして $compile_id
を渡すことができます。
異なる言語でコンパイルされた別々のテンプレートが存在するような、
同じテンプレートの異なるバージョンをコンパイルしたい場合に利用します。
$compile_id の別の利用法としては、複数の
$template_dir
を持っているが
$compile_dir
は1つしかない場合などがあります。各
$template_dir
に別々の $compile_id をセットしなければ、
同名のテンプレートはお互いに上書きされてしまいます。
この関数をコールする度に compile_id を渡す代わりに、一度
$compile_id 変数をセットすることもできます。
Example 13.12. display()
<?php
include(SMARTY_DIR.'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('data', $db_data);
}
// 出力を表示します
$smarty->display('index.tpl');
?>
Example 13.13. display() 関数にテンプレートリソースを指定した例
$template_dir ディレクトリ外のファイルを表示するためには、
テンプレートリソース
を指定します。
<?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');
?>
fetch() および
template_exists()
も参照してください。