Passing multiple entities/collections into Magento email template

Hi friends, my previous article has been written about email sending feature in Magento, and you can read it here. But it seems that we have one more issue – how to display collections/arrays data in the email templates. We’ll try to shed the light on this below.The first thing we should know – is the impossibility to use foreach/for/while in the template. So we should include a block with the template from a current theme inside the email template instead, just like we do for CMS pages. See below:

{{block type='core/template' area='frontend' template='atwix/email/template.phtml'}}

Note, that is a very handy feature, but how do we pass data into the phtml file? – It’s easy:

{{block type='core/template' area='frontend' template='atwix/email/template.phtml' items=$items}}

And if you don’t remember how to pass data into email template, it will be helpful to read our previous articles :)
So, in this case $items variable may be an array or collection, etc…
Then, you just need to sort out your data in phtml-file, see the following example:

<?php foreach ($this->getItems() as $_item): ?>
    <p><?php echo $_item['name'] ?></p>
<?php endforeach; ?>

As a result, you will see a dependence – if you pass items=$items you can get data in a phtml file using $this->getItems() function.

Seems that’s all, but we hope this short Magento article will be useful. Thanks for reading us!