string $flexy->bufferedOutputObject (
object $controllerObject
, array $elements
)
渡されたオブジェクトの変数を配置し、コンパイルされたテンプレートを実行して結果を返します。
PEAR::Cache と組み合わせたり、下記に示すように電子メールテンプレートと組み合わせる事が出来ます (電子メールテンプレートにはまだテストが必要です - 最終的にはバックエンドで電子メールテンプレート用のネイティブトークナイザをサポートする予定です)。
object $controllerObject
-
テンプレートに流し込みたいオブジェクト、オブジェクト変数は $controllerObject->tag にセットされ、テンプレート内の {tag} 変数を置き換える
array $elements
-
フォーム、もしくは dynamic が指定された HTML 要素の name 属性 (または id 属性) による連想配列、テンプレート内に定義された部分とマージされる
string
- テンプレートにオーバーレイされたオブジェクト変数
This function can not be called statically.
Person DataObject send_password method
<?php
class DataObjects_Person {
var $id;
var $name;
var $password;
var $cleartextPassword;
var $email;
function sendEmail($templateFile,$content) {
$content = is_object($content) ? $content : (object) $content;
foreach(get_object_vars($this) as $k=>$v) {
$content->$k = $v;
}
/* <tags をパースしないように regex コンパイラを使用する */
$template = new HTML_Template_Flexy( array(
'compiler' => 'Regex',
'filters' => array('SimpleTags','Mail'),
));
/* テキストファイル (メールテンプレート) をコンパイルする */
$template->compile($templateFile);
/* データ出力にこのオブジェクトの変数を使用する */
$mailtext = $template->bufferedOutputObject($content);
//echo "<PRE>";print_R($mailtext);
/* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
require_once 'Mail/mimeDecode.php';
require_once 'Mail.php';
$decoder = new Mail_mimeDecode($mailtext);
$parts = $decoder->getSendArray();
if (PEAR::isError($parts)) {
return $parts;
}
list($recipents,$headers,$body) = $parts;
$mailOptions = PEAR::getStaticProperty('Mail','options');
$mail = Mail::factory("SMTP",$mailOptions);
return PEAR::isError($mail) ? $mail : $mail->send($recipents,$headers,$body);
}
}
?>
電子メールテンプレート (Regex パーサを使用するため、{t.*} となります)
From: "HTML_Template_Flexy" <[email protected]> Sender: "HTML_Template_Flexy" <[email protected]> To: {t.email} Subject: Here is your new password Content-Type: text/plain; charset=us-ascii Dear {t.name} Your New Password is {t.cleartextPassword} please use it to log into your bank account :)
結果として得られるメールヘッダと本文
From: "HTML_Template_Flexy" <[email protected]> Sender: "HTML_Template_Flexy" <[email protected]> To: [email protected] Subject: Here is your new password Content-Type: text/plain; charset=us-ascii Dear Fred Blobs Your New Password is 0ab123dcc please use it to log into your bank account :)