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 /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Using tempalte parser renderWith by string possible?

Go to End

2 Posts   1579 Views


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

        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();
                'Content' => $content


Community Member, 7 Posts

28 January 2016 at 11:29am

Haven't tried this but should work
renderWith(SSViewer::fromString($content, true) ); // true if you want the template to be cached