JohnT
JohnT

Reputation: 967

How to loop through a variables in Magento Email Template?

Let's say I've the following variables to pass to my Email Template:

$vars = array(
    'products'  => $products,
);

Where $products is a collection, how could I iterate over this collection in Email template?

Upvotes: 3

Views: 5763

Answers (2)

pspahn
pspahn

Reputation: 2790

The above works, but an alternative would be to let your XML do the work by calling the layout handle in the email template:

{{layout handle="email_stuff"}}

In your local.xml or module.xml or where ever you like:

<email_stuff>
    <block type="yourblock/type" name="email_stuff" template="path/to/template.phtml" />
</email_stuff>

I guess the main difference is where you're doing most of your email "work". I've used this method for loading in email headers/footers that stay the same in each template. The previous answer is likely simpler for basic tasks, however.

Upvotes: 1

Joe Mastey
Joe Mastey

Reputation: 27119

I don't believe that Magento's templating engine is clever enough to do loops. Instead, use an inline block, as Magento does for order items. Something like this:

{{block type='core/template' area='frontend' template='path/to/your/template.phtml' products=$products}}

Hope that helps!

Thanks, Joe

Upvotes: 6

Related Questions