string $flexy->bufferedOutputObject (
object $controllerObject
, array $elements
)
This maps the values of the supplied object and runs the compiled template, and returns the result.
This can be used in conjuction with PEAR::Cache, or in the example below, with a email template (note this still needs testing.. - the backend should eventually support a native tokenizer for email templates.)
object $controllerObject
-
The object you want to use with the template, the values of the object will
relate to the $controllerObject->tag will map to {tag} on the template
array $elements
-
This is an associative array of form, or dynamic elements names (or id's) which will be merged with
the one defined in the template.
string
- the object variables overlayed on the template
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;
}
/* use the regex compiler, as it doesnt parse <tags */
$template = new HTML_Template_Flexy( array(
'compiler' => 'Regex',
'filters' => array('SimpleTags','Mail'),
));
/* compile a text file (email template) */
$template->compile($templateFile);
/* use variables from this object to ouput data. */
$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);
}
}
?>
An email template (using the Regex Parser - hence the {t.*})
From: "HTML_Template_Flexy" <html_template_flexy@pear.php.net> Sender: "HTML_Template_Flexy" <html_template_flexy@pear.php.net> 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 :)
The resulting email head and body
From: "HTML_Template_Flexy" <html_template_flexy@pear.php.net> Sender: "HTML_Template_Flexy" <html_template_flexy@pear.php.net> To: demo@example.com 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 :)