Jump to:

3397 Posts in 978 Topics by 854 members

Template Questions

SilverStripe Forums » Template Questions » Help Email populateTemplate

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

Page: 1
Go to End
Author Topic: 1552 Views
  • Peter_SS
    Avatar
    Community Member
    19 Posts

    Help Email populateTemplate Link to this post

    I'm trying to pass a DataObjectSet and using <% control MyDataObjectSet %> inside my EmailTemplate.SS but it's not working. I've also tried passing an Array and using also control blocks but still no luck. any suggestions?

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: Help Email populateTemplate Link to this post

    Could you post the code you have currently?

  • Peter_SS
    Avatar
    Community Member
    19 Posts

    Re: Help Email populateTemplate Link to this post

    <?php
    class Notification extends DailyTask {

       function process() {
          $expiringDocs = new DataObjectSet();
          $newDOSet = new DataObjectSet();
          foreach( $expiringDocs as $docs ) {
             $member = DataObject::get_by_id('Member', (int)$docs->PersonID );
             $name = $member->Surname .', '. $member->FirstName;
             $record = array(
                'FullName' => $name,
                'Type' => $docs->Type,
             );
             $newDOSet ->push(new ArrayData($record));
          }
          $this->sendEmail($newDOSet);
       }

    function sendEmail($data) {
        $from = 'email@mydomain.com';
        $to = 'customer@yahoo.com';
        $subject = 'Documents Notification';
    $email = new Email($from, $to, $subject);
    $email->setTemplate('NotificationTemplate');
        $email->populateTemplate($data);
        $email->send();
    }
    }

    NotificationTemplate.ss
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html lang="en">
       <head>
          <title></title>
          </head>
          <body>
    <% control data %>
           <p>$FullName</p>
            <p>$Type</p>
    <% end_control %>
       </body>
    </html>

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: Help Email populateTemplate Link to this post

    I think the issue is that you're using <% control data %> - $data was just the variable passed to the populate template. You should pass the populateTemplate() function an array in the format of key => value like so..

    $email->populateTemplate(array('MyData' => $data));

    Then use <% control MyData %> in the template since that is now the name of $data.

  • Peter_SS
    Avatar
    Community Member
    19 Posts

    Re: Help Email populateTemplate Link to this post

    thanks Willr. I'll try that

    1552 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.