W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
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()
.
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: