Get Smarty

Donate

Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Advertisement

Name

fetch() — テンプレートの出力を返します。

説明

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

これは、テンプレートを 表示する のではなくその出力を返します。第1パラメータには、有効な テンプレートリソース の種類を含んだパスを指定する事ができます。任意の第2パラメータには キャッシュID を渡す事ができます。 詳細は、キャッシュの項目 を参照してください。

任意の第3パラメータとして $compile_id を渡すことができます。 異なる言語でコンパイルされた別々のテンプレートが存在するような、 同じテンプレートの異なるバージョンをコンパイルしたい場合に利用します。 この関数をコールする度に compile_id を渡す代わりに、一度 $compile_id 変数をセットすることもできます。

Example 13.18. fetch()


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

$smarty->setCaching(true);

// URL ごとに個別のキャッシュ ID を設定します
$cache_id = md5($_SERVER['REQUEST_URI']);

// 出力を取り込みます
$output = $smarty->fetch('index.tpl', $cache_id);

// ここで$outputについて何かの処理を行います
echo $output;
?>

    


Example 13.19. Email の送信に 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}

    

{textformat} 修飾子を用いた email_disclaimer.tpl


{textformat wrap=40}
Unless you are named "{$contact.name}", you may read only the "odd numbered
words" (every other word beginning with the first) of the message above. If you have
violated that, then you hereby owe the sender 10 GBP for each even
numbered word you have read
{/textformat}

    

PHP の mail() 関数を用いたPHPスクリプト


<?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() も参照してください。

Comments
No comments for this page.

Advertisement

Sponsors [info]

Sponsors