fetch()
返回模板输出
说明
string
fetch
string
模板
string
cache_id
string
compile_id
这将返回模板输出而不显示它。提供有效的模板资源类型和路径。作为可选的第二个参数,你可以传递一个$cache id
,有关更多信息请参阅缓存部分。
PARAMETER.COMPILEID
<?php
use Smarty\Smarty;
$smarty = new Smarty;
$smarty->setCaching(true);
// set a separate cache_id for each unique URL
$cache_id = md5($_SERVER['REQUEST_URI']);
// capture the output
$output = $smarty->fetch('index.tpl', $cache_id);
// do something with $output here
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}
使用 PHPmail()
函数的 PHP 脚本
<?php
// get $contact_info from db or other resource here
$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()
。