display()

display() -- 显示模板

描述

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

    该函数显示模板(与fetch()不同)。需要提供一个合法的模板资源类型和路径。作为第二个可选参数,你可以提供传递一个$cache id,其更多信息参见缓存一章。

    作为第三个可选参数,你可以传递一个$compile_id。此时你可能想对同一个模板编译不同版本,如对于不同的语种编译不同的编译模板。另一个使用$compile_id的情形是你有多个$template_dir但只有一个$compile_dir。为每个$template_dir设置一个独立的$compile_id,否则同名的模板会相互覆盖。你也可以一次性设置$compile_id变量而不用每次调用该函数时传递该参数。

例子13-1. display()

<?php
include(SMARTY_DIR.'Smarty.class.php');
$smarty = new Smarty();
$smarty->caching true;

// 只有在缓存不存在时才进行数据库调用
if(!$smarty->is_cached('index.tpl')) {

  
// dummy up some data
  
$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');
?>

    参见fetch()template_exists()