Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions /

Using tempalte parser renderWith by string possible?


Reply


269 Views

Avatar
m-phil

Community Member, 37 Posts

1 May 2014 at 9:10pm

Edited: 01/05/2014 9:42pm

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();