For sending special emails I have added an email content field ("HTMLText") to siteconfig settings. Now I want to give me the possibility to use something like $Member.Surname within these field instead of using an email template.
My current solution works by replacing these objects using:
/**
* Parse object references i.e. $Campaign.Title to "Your campaign name"
*
* @param $string HTMLText
* @return string
*/
private function parseString($string) {
preg_match_all("/[\$]{1}[\w]+\.[\w]+/", $string, $match); // Find i.e. $Campaign.Title
if(count($match[0])) {
foreach($match[0] as $key) {
$objects = array('Campaign', 'Task', 'Member'); // Parsing only these DataObjects
foreach($objects as $class) {
if(preg_match("/[\$]{1}".$class."/", $key)) {
$parts = explode(".", $key);
if(!$parts) continue;
$name = $parts[1];
$object = $this->{strtolower($class)};
$value = $object->{$name};
$string = str_replace($key, $value, $string);
continue;
}
}
}
}
return $string;
}
but now I need <% loop %> :-) perhaps I can use something from the given functions like ViewableData->renderWith() by transforming HTMLText to a temporary template or you can tell me the "deeper" method to use my string directly?
Only for information, I also use an basic email template to populate my $Content
$content = $this->parseString($string);
// Create mail
$email = new Email();
$email->setFrom($siteConfig->SystemMailAddress);
$email->setTo($this->member->Email);
$email->setSubject($subject);
$email->setTemplate($templateDir.$type.'Email');
$email->populateTemplate(
array(
'Content' => $content
)
);
$email->send();