Smarty成員方法:fetch()

2018-12-02 15:01 更新

Name

fetch() — 取得輸出內(nèi)容

說明

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

該函數(shù)將取得一個(gè)模板輸出的內(nèi)容,而不是顯示出來。 該函數(shù)需要指定一個(gè)合法的模板資源的類型和路徑。 第二個(gè)可選的參數(shù)$cache_id設(shè)置緩存,詳情參見 緩存。

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生成一個(gè)特定的緩存ID
$cache_id = md5($_SERVER['REQUEST_URI']);

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

// 處理輸出的內(nèi)容
echo $output;
?>


Example 14.22. 用fetch()來發(fā)郵件

email_body.tpl模板的內(nèi)容:

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}

該P(yáng)HP腳本使用了PHP的 mail()函數(shù)。

<?php

// 從數(shù)據(jù)庫或其他來源獲取到$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().

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號