There are two logical directives that can be used in Magento 2 email templates. The depend
and the
if
directives. I will explain both in this post.
We start the opening tag with two opening curly brackets {{
followed by the string
depend
, whitespace , a variable and two closing curly brackets }}
. The closing tag
looks like this {{/depend}}
. Everything between the opening and the closing tag will be
printed or executed to the template if the variable of the opening part exists and is not similar to an empty string
in the template. Here a small example from Magento_Sales::email/creditmemo_new_guest.html
.
{{depend store_hours}}
{{trans 'Our hours are <span class="no-link">%store_hours</span>.' store_hours=$store_hours |raw}}
{{/depend}}
The part between {{depend store_hours}}
and {{/depend}}
is
only printed or executed if the variable store_hours
exists.
The if
is similar to the depend
directives, but it has a else
tag if needed.
We start the opening tag with two opening curly brackets {{
followed by the string
if
, whitespace, a variable and two closing curly brackets }}
. The middle tag is just
{{else}}
and not required. The closing tag looks like this
{{/if}}
. Everything between the opening tag and the else tag will be printed or executed if
the variable from above exists and is not similar to an empty string. In all other cases the part between the else
tag and the closing tag will be printed or executed. Here a small example with and else tag from
Magento_Email::email/header.html
.
<a class="logo" href="{{store url=""}}">
<img
{{if logo_width}}
width="{{var logo_width}}"
{{else}}
width="180"
{{/if}}
{{if logo_height}}
height="{{var logo_height}}"
{{else}}
height="52"
{{/if}}
src="{{var logo_url}}"
alt="{{var logo_alt}}"
border="0"
/>
</a>
The if directives works also without else tag. In that case it is exactly the same as the depends directives. Here
a small example for a if without a else from Magento_Sales::email/order_new_guest.html
.
{{if shipping_msg}}
<p>{{var shipping_msg}}</p>
{{/if}}
You may have one of the following questions now.
if
or depend
operators?depend
the same as if
without else
?if
in a depend
and the other way around, but It is impossible to
use a if
in a if
or a depend
in a depend
. The reason is pretty
easy, Magento uses just simple regex to parse the templates and the closing tag of a child if is for the regex the
closing tag of the parent tag. I guess that's the reason why Magento has also the depend
directive.
With the if
or depend
operators and the feature to
injecting variables into child email templates
we can control the behavior for child templates. Let us assume that we want a special paragraph in the footer
template, but only if we send the newsletter subscription for example. Our footer template could look like this for
example.
<!--
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<!--@subject {{trans "Footer"}} @-->
<!--@vars {
"var store.getFrontendName()":"Store Name"
} @-->
<!-- End Content -->
</td>
</tr>
<tr>
<td class="footer">
<p class="closing">{{trans "Thank you, %store_name" store_name=$store.getFrontendName()}}!</p>
</td>
{{depend is_newsletter}}
<p>Here is a special paragraph only for the newsletter subscription</p>
{{/depend}}
</tr>
</table>
</td>
</tr>
</table>
<!-- End wrapper table -->
</body>
We can control the output of the newsletter special paragraph from the parent template with the variable
is_newsletter
. If we want to show the newsletter paragraph we can use the following code in the parent
email template for example.
{{template config_path="design/email/footer_template" is_newsletter="true"}}
true
as example value for is_newsletter
.
In every other parent template we can just ignore the variable is_newsletter
and the newsletter special
paragraph will not be printed in the footer.
{{template config_path="design/email/footer_template"}}