Jump to:

3447 Posts in 1032 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » Using tempalte parser renderWith by string possible?

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

Page: 1
Go to End
Author Topic: 232 Views
  • m-phil
    Avatar
    Community Member
    37 Posts

    Using tempalte parser renderWith by string possible? Link to this post

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

    232 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.