Smarty成员方法:fetch()

Name

fetch() — 取得输出内容

说明

string fetch(string template,
             string cache_id,
             string compile_id);

该函数将取得一个模板输出的内容,而不是显示出来。 该函数需要指定一个合法的模板资源的类型和路径。 第二个可选的参数$cache_id设置缓存,详情参见 缓存。

As an optional third parameter, you can pass a $compile_id. This is in the event that you want to compile different versions of the same template, such as having separate templates compiled for different languages. You can also set the$compile_id variable once instead of passing this to each call to this function.

Example 14.21. fetch()

<?php
include('Smarty.class.php');
$smarty = new Smarty;

$smarty->setCaching(true);

// 按照URL来MD5生成一个特定的缓存ID
$cache_id = md5($_SERVER['REQUEST_URI']);

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

// 处理输出的内容
echo $output;
?>


Example 14.22. 用fetch()来发邮件

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脚本使用了PHP的 mail()函数。

<?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().

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部