SEARCH

このマニュアルはSmartyのものです。セキュリティなどの問題でRCMSでは利用できないものもありますので、ご注意ください。
fetch()

fetch()

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

説明

string fetch ( string template [, string cache_id [, string $compile_id]])

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

任意の第3パラメータとして $compile_id を渡すことができます。 異なる言語でコンパイルされた別々のテンプレートが存在するような、 同じテンプレートの異なるバージョンをコンパイルしたい場合に利用します。 $compile_id の別の利用法としては、複数の $template_dir を持っているが $compile_dir は1つしかない場合などがあります。各 $template_dir に別々の $compile_id をセットしなければ、 同名のテンプレートはお互いに上書きされてしまいます。 この関数をコールする度に compile_id を渡す代わりに、一度 $compile_id 変数をセットすることもできます。

例 13-1. fetch()

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

$smarty->caching true;

// キャッシュが存在しない場合はデータベースを呼び出します
if(!$smarty->is_cached('index.tpl')) {

  
// ダミーデータを用意
  
$address '245 N 50th';
  
$db_data = array(
               
'City' => 'Lincoln',
               
'State' => 'Nebraska',
               
'Zip' => '68502'
             
);

  
$smarty->assign('Name','Fred');
  
$smarty->assign('Address',$address);
  
$smarty->assign($db_data);

}

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

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

例 13-2. Email の送信に fetch() を使用する

email_body.tpl テンプレート

Dear {$contact.name},

Welcome and thankyou for signing up as a member of our user group,

Click on the link below to login with your user name of '{$contact.login_id}'
so you can post in our forums.

http://{$smarty.server.SERVER_NAME}/login/

List master
Some user group

{include file='email_disclaimer.tpl'}

{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

// pear や adodb などを使用して、データベースから連絡先を取得します
$query  'select name, email, login_id from contacts where contact_id='.$contact_id;
$contact $db->getRow($sql);
$smarty->assign('contact'$contact);

mail($contact['email'], 'Subject'$smarty->fetch('email_body.tpl'));

?>

{fetch}display(){eval}、 および template_exists() も参照してください。

SEARCH