display()
显示模板
描述
void
display
string
template
string
cache_id
string
compile_id
此函数会显示模板的内容。要将模板的内容返回到一个变量中,使用fetch()。提供一个有效的模板资源类型和路径。作为可选的第二个参数,你可以传递一个$cache_id,更多信息请查看缓存部分。
参数.COMPILEID
<?php
include(SMARTY_DIR.'Smarty.class.php');
$smarty = new Smarty();
$smarty->setCaching(true);
// 如果缓存不存在,则只执行数据库调用
if(!$smarty->isCached('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');
?>
使用模板资源的语法来显示$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() 和
templateExists().