fetch()
返回模板输出
描述
string
fetch
string
template
string
cache_id
string
compile_id
此方法返回模板的输出,而不是显示它。提供一个有效的模板资源类型和路径。作为可选的第二个参数,你可以传递一个$cache id,查看缓存部分获取更多信息。
PARAMETER.COMPILEID
<?php
include('Smarty.class.php');
$smarty = new Smarty;
$smarty->setCaching(true);
// 为每个唯一的URL设置一个独立的cache_id
$cache_id = md5($_SERVER['REQUEST_URI']);
// 捕获输出
$output = $smarty->fetch('index.tpl', $cache_id);
// 在这里使用$output做一些事情
echo $output;
?>
email_body.tpl 模板
Dear {$contact_info.name},
Welcome and thank you for signing up as a member of our user group.
Click on the link below to login with your user name
of '{$contact_info.username}' so you can post in our forums.
{$login_url}
List master
{textformat wrap=40}
This is some long-winded disclaimer text that would automatically get wrapped
at 40 characters. This helps make the text easier to read in mail programs that
do not wrap sentences for you.
{/textformat}
使用 PHP mail() 函数的 php 脚本
<?php
// 在这里从数据库或其他资源获取$contact_info
$smarty->assign('contact_info',$contact_info);
$smarty->assign('login_url',"http://{$_SERVER['SERVER_NAME']}/login");
mail($contact_info['email'], 'Thank You', $smarty->fetch('email_body.tpl'));
?>
参见 {fetch}
display(), {eval}, 和
templateExists()。