Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 489
EOB Avatar asked Dec 27 '22 07:12

EOB


1 Answers

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.

like image 102
Jürgen Thelen Avatar answered Jan 17 '23 16:01

Jürgen Thelen