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 /

Help Email populateTemplate


Reply


6 Posts   1959 Views

Avatar
Peter_SS

Community Member, 19 Posts

29 August 2010 at 6:45am

Edited: 29/08/2010 6:45am

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?

Avatar
Willr

Forum Moderator, 5513 Posts

29 August 2010 at 2:11pm

Could you post the code you have currently?

Avatar
Peter_SS

Community Member, 19 Posts

29 August 2010 at 5:35pm

Edited: 29/08/2010 5:49pm

<?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>

Avatar
Willr

Forum Moderator, 5513 Posts

30 August 2010 at 8:55am

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.

Avatar
Peter_SS

Community Member, 19 Posts

30 August 2010 at 11:51pm

thanks Willr. I'll try that

Avatar
merrick_sd

Community Member, 97 Posts

13 June 2014 at 10:22pm

Edited: 24/06/2014 2:17am

This post has really helped

the below works, but I'm now thinking what about if i wanted to use the user_forms module.

Will I need to figure out how to extend user_forms in order that i can populate extra data to the userforms SubmittedFormEmail.ss template?

I'm asking just in case there is an alternative way of accessing page_controller functions in email templates, that I'm missing.

mysite > code > Pag.php
So if i have a function in my Page_Controller

public function RegisteredOffice(){
               return Office::get()->filter(array('MainOffice' => 1));
             //old return DataObject::get("Office", "MainOffice = 1");
            }

themes > templates > Page.ss
Then on my Page.ss i can say

<% loop RegisteredOffice %> $Address1 <% end_loop %>

That works
------------

My question is how should I access this RegisteredOffice fuction in my Email Template ContactEmail.ss

Do I have to add it to my $emaildata and then populateTemplate (see code below 'RegisteredOffice' => Page_Controller::RegisteredOffice(), )

ContactPage > code > ContactPage.php

function SendContactForm($data, $form)
   {
   
   $DataContactSubmission = new ContactSubmission();

$DataContactSubmission->SiteTreeID = $this->ID;

//echo "<p>email: " . $DataContactSubmission->getField('Email') . "</p>";
//   die ('<pre>' . print_r($DataContactSubmission, true) . '</pre>');
$form->saveInto($DataContactSubmission);
//$form->saveInto($Member);
//why do i need this but dont on other example
// $DataContactSubmission->write();
//$Member->login();
if ($DataContactSubmission->write()) {

       //Set data
      $From = $data['Email'];
      $To = $this->Mailto;
      $Subject = "CCSW: Website Contact message";    
      $email = new Email($From, $To, $Subject);
      //set template
      $email->setTemplate('ContactEmail');
      
       //Put form data into an array
       //Clean up returns
      
       $emaildata = array(
                   'Prefix' => $data['Prefix'],
                   'FirstName' => $data['FirstName'],
                   'Surname' => $data['Surname'],
                  'Company' => $data['Company'],
                   'DaytimeTel' => $data['DaytimeTel'],
                'EveningTel' => $data['EveningTel'],
                   'Email' => $data['Email'],
                  'ServiceEnquiry' => $data['ServiceEnquiry'],
                  'Comments' => str_replace("\n", "<br>", $data['Comments']),
                  'RegisteredOffice' => Page_Controller::RegisteredOffice(),
// 'SiteConfig' => SiteConfig::current_site_config()
);
               
      //populate template
      $email->populateTemplate($emaildata);
      //send mail
      $email->send();
      
      } else {
         $form->sessionMessage(_t('VALUATION_REQUEST_SUBMIT_ERROR', 'There was an error'), 'error');
      }
      
       //return to submitted message
      
      //return Director::redirect(Director::baseURL(). $this->URLSegment . "/?success=1");
         return $this->redirect(Director::baseURL(). $this->URLSegment . "/?success=1");

   }

themes > templates > Email > ContactEmail.ss

<% loop RegisteredOffice %>
$Address1
<% end_loop %>

I'm after a tutorial on how to extend userforms rather than edit line 985 of UserDefinedForm.php

..sometime later

Final Answer:

In case it helps anyone else

mysite >_config.php

Object::add_extension('UserDefinedForm_Controller', 'MyUserDefinedFormExtension');

mysite > code > MysUserDefinedFormExtension.php

<?php
/**
* An Extension to the UserDefinedForm submission process
*
* @package userforms
* Reasoning:
* 1. do not touch source code of userforms module.
* 2. I want to pass data which is captured via the functions in my Page_Controller into the SubmittedFormEmail.ss template
      eg <% loop RegisteredOffice %> $Adddress1 <% end_loop %>
*/

/*
Mysite > _config.php contains the following to register this Extended Class
Object::add_extension('UserDefinedForm_Controller', 'MyUserDefinedFormExtension');

*/
class MyUserDefinedFormExtension extends Extension {
   
   
   private static $allowed_actions = array(
      'updateEmailData'
   );

   public function updateEmailData($emailData, $attachments){
      
      $attachments = null;
      $registeredoffices = Controller::curr()->RegisteredOffice();
      $footericons = Controller::curr()->getAllFooterIcons();
      $NewData = $emailData;
      $NewData["RegisteredOffice"] = $registeredoffices;
      $NewData["AllFooterIcons"] = $footericons;
      $emailData = $NewData;
      $this->emailData = $NewData;

   return $this->emailData; //return emailData
      
   }
   
   public function updateEmail($email, $recipient, $emailData){
   
      $emailData = $this->emailData;
      $email->populateTemplate($emailData);
      
   return;
      
   }
   
}

also at http://www.sspaste.com/paste/show/53a82f953bf20