display() テンプレートを表示します

説明

void display ( string template [, string cache_id [, string compile_id]])

パラメータ

stringtemplateテンプレート名
stringcache_idキャッシュID/td>
stringcompile_idコンパイルファイルID

テンプレートを表示します (fetch() と違い出力を行います)。 第1パラメータには、有効な テンプレートリソース の種類を含むパスを指定する事ができます。 任意の第2パラメータには キャッシュID を渡す事ができます。 詳細は キャッシュの項 を参照してください。 任意の第3パラメータとして $compile_id を渡すことができます。 異なる言語でコンパイルされた別々のテンプレートが存在するような、同じテンプレートの異なるバージョンをコンパイルしたい場合に利用します。 $compile_id の別の利用法としては、複数の $template_dir を持っているが $compile_dir は1つしかない場合などがあります。 各 $template_dir に別々の $compile_id をセットしなければ、同名のテンプレートはお互いに上書きされてしまいます。 この関数をコールする度に compile_id を渡す代わりに、一度 $compile_id 変数をセットすることもできます。

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');
例 13-2. 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');

関連記事

スポンサーリンク

phpMyAdminで『information_schema』などを非表示にする方法

ホームページ製作・web系アプリ系の製作案件募集中です。

上に戻る