Loading a Magento mail template and fill its vars from code?

徘徊边缘 提交于 2019-12-06 05:04:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!