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

Help Email populateTemplate

Go to End

6 Posts   4121 Views


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?


Forum Moderator, 5523 Posts

29 August 2010 at 2:11pm

Could you post the code you have currently?


Community Member, 19 Posts

29 August 2010 at 5:35pm

Edited: 29/08/2010 5:49pm

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

function sendEmail($data) {
$from = [email protected]';
$to = [email protected]';
$subject = 'Documents Notification';
$email = new Email($from, $to, $subject);
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "">
<html lang="en">
<% control data %>
<% end_control %>


Forum Moderator, 5523 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.


Community Member, 19 Posts

30 August 2010 at 11:51pm

thanks Willr. I'll try that


Community Member, 99 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 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 >
Then on my i can say

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

That works

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

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>');
       //why do i need this but dont on other example
      // $DataContactSubmission->write();
  if ($DataContactSubmission->write()) {      
	 	//Set data
		$From = $data['Email'];
		$To = $this->Mailto;
		$Subject = "CCSW: Website Contact message";  	  
		$email = new Email($From, $To, $Subject);
		//set template
		  //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
		//send mail
		} 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 >

<% loop RegisteredOffice %>
<% 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

 * 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 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(

	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;

also at