Magento 2 email template block directives

This post tries to explain every possible way to use blocks in Magento 2 email templates.

Including blocks

It is possible to include blocks in our email templates. In general we just need to specify the class of a block. Let us assume that we have the following template under the path Lesti_Example::email/exampletemplate.phtml.

<?php
echo "Hello, I'm a PHP template";

We can include this template in our email template with the following code.

{{block class='Magento\\Framework\\View\\Element\\Template' area='frontend' template='Lesti_Example::email/exampletemplate.phtml'}}

Please note, we can also use more advanced class blocks. That way it should be easy to create a small product list in your email templates. We can also inject variables from our email template into the block. For example Magento_Sales::email/shipment_new.html is injecting $shipment and $order into template Magento_Sales::email/shipment/track.phtml.

{{block class='Magento\\Framework\\View\\Element\\Template' area='frontend' template='Magento_Sales::email/shipment/track.phtml' shipment=$shipment order=$order}}

Magento will always use the toHtml function to render a block. But you can also specify a output function with the parameter output. Every parameter that is not class, id or output will be automatically injected into the block.

Including CMS blocks

Sometimes called static blocks, content blocks or CMS blocks. CMS blocks are a special case. Magento will always render a Magento\Cms\Block\Block if no class parameter is given. You will just need the id parameter. Let us assume we have the following CMS block in our database.

keyvalue
ID51
Content<h1>Hello, I'm a CMS testblock</h1>

We want to include this CMS block in our email template. We can do this with the following code in our email Template Content.

{{block id="51"}}
Next Previous