问题
I am loading my mail template like this:
$mailTemplate = Mage::getModel('core/email_template');
$myTemplate = $mailTemplate->load($templateId);
Now I can get the template content using:
$text = $myTemplate ->getData('template_text');
This works, but $text still contains the placeholders for the variables, like {{var myvar}} or {{store url=""}}. Is there a way to fill those placeholders when loading the template without sending the mail? I want to show the text to the user, but with filled placeholders.
Possible?
Thanks :)
回答1:
Yes, it's possible.
The class Mage_Core_Model_Email_Template
has a method getProcessedTemplate()
. You only need to pass along the proper variables for your placeholders.
For example, if your template contains placeholders like this:
{{var firstname}} {{var lastname}}
you can use:
$sTemplate = Mage::getModel('core/email_template')
->load($templateId)
->getProcessedTemplate(array(
'firstname' => 'John',
'lastname' => 'Doe'
));
to get your placeholders resolved.
回答2:
To load an email template and fill it with variables you can do the following:
$emailTemplate = Mage::getModel('core/email_template')
->loadDefault('<your_email_template>');
//create template variables
$emailTemplateVariables = array();
$emailTemplateVariables['firstname'] = <firstname_var>;
$emailTemplateVariables['lastname'] = <lastname_var>;
// in your tample tou can use {var firstname} and {var lastname}
//fill template variables in email template
$processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);
$emailTemplate->setSenderName('<name>');
$emailTemplate->setSenderEmail('<emailaddress>');
$emailTemplate->setTemplateSubject($this->__('<your subject>'));
//send mail
$emailTemplate->send(<receiver_emailaddress>, <receiver_name>, $emailTemplateVariables);
Hopefully someone could use it ;-)
Kind regards,
Martijn
来源:https://stackoverflow.com/questions/9017951/loading-a-magento-mail-template-and-fill-its-vars-from-code