codecamp

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

Smarty成员方法:enableSecurity()
Smarty成员方法:getCacheDir()
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

I.Smarty基础

1.Smart是什么?

II.Smarty模板设计师篇

6.Smarty复合修饰器

9.Smarty配置文件

10.Smarty调试控制台

III. 程序开发者篇

11. Smarty字符集编码

12.Smarty常量

13.Smarty成员变量

14.Smarty成员方法

17.Smarty高级特性

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }