display()
显示模板
描述
void
display
string
template
string
cache_id
string
compile_id
这将显示模板的内容。 若要将模板的内容返回到变量中,请使用 fetch()
。 提供一个有效的 模板资源 类型和路径。 可以将 $cache_id
作为可选的第二个参数传递,有关详细信息,请参见 缓存部分。
PARAMETER.COMPILEID
<?php
use Smarty\Smarty;
$smarty = new Smarty();
$smarty->setCaching(true);
// only do db calls if cache doesn't exist
if(!$smarty->isCached('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);
}
// display the output
$smarty->display('index.tpl');
?>
使用 模板资源 的语法在 $template_dir
目录以外显示文件。
<?php
// absolute filepath
$smarty->display('/usr/local/include/templates/header.tpl');
// absolute filepath (same thing)
$smarty->display('file:/usr/local/include/templates/header.tpl');
// windows absolute filepath (MUST use "file:" prefix)
$smarty->display('file:C:/www/pub/templates/header.tpl');
// include from template resource named "db"
$smarty->display('db:header.tpl');
?>
另请参见 fetch()
和 templateExists()
。