Jump to:

22977 Posts in 11806 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Email Template Variable Scoping

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

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

Page: 1
Go to End
Author Topic: 311 Views
  • cwchong
    Avatar
    Community Member
    13 Posts

    Email Template Variable Scoping Link to this post

    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?

  • cwchong
    Avatar
    Community Member
    13 Posts

    Re: Email Template Variable Scoping Link to this post

    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.

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Email Template Variable Scoping Link to this post

    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.

  • cwchong
    Avatar
    Community Member
    13 Posts

    Re: Email Template Variable Scoping Link to this post

    Yeah, got it Will, tks for the advise.

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