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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Email Template Variable Scoping


Go to End


4 Posts   844 Views

Avatar
cwchong

Community Member, 13 Posts

15 September 2012 at 8:54am

Hi, have a question pertaining to the variable scope for email templates:

$mydo = new MyDataObject(); // assume prepopulated; contained a data field named "Subject" accessed by $mydo->Subject
$From = "sender@email.com";
$To = "receiver@email.com";
$Subject = "My Email Subject";
$email = new Email($From, $To, $Subject);
$email->setTemplate('TemplateEmail');
$email->populateTemplate($mydo);
$email->send();

The part that I am having issues with is the dataobject's field and the declared variable "$Subject"
In my "TemplateEmail", when I tried to access "$Subject", it should be rightfully accessing the value stored in $mydo->Subject, but what I am experiencing atm is the value stored as "My Email Subject" being printed in the template instead;
The code in the template for accessing the data is simply

$Subject.XML

Am I missing anything?

Avatar
cwchong

Community Member, 13 Posts

15 September 2012 at 9:09am

I guess $Subject is a reserved variable for the email template to reference the actual email subject, regardless whether the variable is passed into the template.

Not sure if there's a way to override that, but I just changed the variable in my dataobject so that it doesn't name clash.

Avatar
Willr

Forum Moderator, 5523 Posts

15 September 2012 at 11:39am

I'm picking Email::getSubject() will override your Subject value on your model. A work around would be to define a getter on your DataObject

function getEmailSubject() {
return $this->dbObject('Subject');
}

Then use $EmailSubject so it won't clash.

Avatar
cwchong

Community Member, 13 Posts

15 September 2012 at 11:53am

Yeah, got it Will, tks for the advise.