fetch() -- 返回模板输出


string fetch ( string template [, string cache_id [, string $compile_id]])



例子13-1. fetch()

$smarty = new Smarty;

$smarty->caching true;

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

// 随便填一些数据
$address '245 N 50th';
$db_data = array(
'City' => 'Lincoln',
'State' => 'Nebraska',
'Zip' => '68502'



// 捕获输出
$output $smarty->fetch('index.tpl');

// 对$output进行一些处理
echo $output;

例子13-2. 使用fetch()来发送邮件


Dear {$contact.name},

Welcome and thankyou for signing up as a member of our user group,

Click on the link below to login with your user name of '{$contact.login_id}'
so you can post in our forums.


List master
Some user group

{include file='email_disclaimer.tpl'}


{textformat wrap=40}
Unless you are named "{$contact.name}", you may read only the "odd numbered
words" (every other word beginning with the first) of the message above. If you have
violated that, then you hereby owe the sender 10 GBP for each even
numbered word you have read



// 从数据库中获得联系人信息
$query  'select name, email, login_id from contacts where contact_id='.$contact_id;
$contact $db->getRow($sql);

mail($contact['email'], 'Subject'$smarty->fetch('email_body.tpl'));