Using Conditions (if, else, for) in Email Templates
With conditions in emails, you can customize the text based on factors such as the status of an order or the shipping provider used.
The placeholders used in the examples are not available for all types of emails. Check the available placeholders for:
If Condition: If Statement
An if-statement allows you to display text if a specific condition is true.
Example
If the remark in the purchase order contains text (length greater than 0), display the remark.
{{ purchaseorder.remarks }}
{% endif %}
If-Else Condition: If-Else Statement
If the condition is true, display one text; otherwise, display another text.
Example
If the shipping provider is PostNL, show the tracking link; otherwise, display the text for picking up the package.
Track your package here: https://your.postnl.nl/#!/track-and-trace/search/
{% else %}
Your package is ready for pickup at Dorpstraat 33 in Klein Veendam.
{% endif %}
Displaying Lists
Some placeholders in emails are lists or arrays. You can use a for-statement to create a list in the email.
Example
{{ product.amount }}x {{ product.name }} ({{ product.productcode}})
{% endfor %}
This will appear in the email:
1x Honig Standard Triple (AT807917)
1x Mars Lumix Graphing (ZC360355)
- {{ shipment.trackingcode }} | {{ shipment.trackingurl}}
{% endfor %}
This will appear in the email:
- https://example.org/tracking/3S9399232 | 3S9399232
- https://example.org/tracking/3S9981273 | 3S9981273
Available Operators for Conditions
Check if something is true (otherwise false)
Check if the placeholder contains a value
Check if a placeholder has a specific value
Whitespace
Using if-statements and variables at the beginning or end of a line can result in too many or too few blank lines in the final email. To fix this, Picqer automatically removes extra blank lines and consolidates them into a single blank line. This allows you to use blank lines in the template for visual clarity without affecting the final email output.
You can also use a - or ~ in tags to control whitespace specifically. Tags like {%- if has_shipment %} will remove any blank lines before it, while {%~ if has_shipment %} ensures that all whitespace is preserved. For more tips on this, refer to this Twig blog post.
Good to Know
- You can use all placeholders in an email for the conditions.
- If or if-else statements must always be closed with {% endif %} to mark the end of the condition.
- For-statements must always be closed with {% endfor %} to mark the end of the list iteration.
Filters
You can also combine conditions with filters. This allows you to, for example, convert text to uppercase or count the length of a string.