Q: How do I solve the error showing the Unexpected char '&'?
Explanation:
When an email fails to render and gets stuck in the loading phase with an error, it means there is a blank space in the code where it is not expected. Jinja syntax is sensitive to such errors and fails to execute.
Additionally, if the error starts with Unexpected char ‘&’, check the error message to locate the part of the code where the character ‘&’ is contained. For example, in the above image, the blank space (indicated by “ ” or non-breaking space) is between the number 5 and the enclosing of the jinja expression brackets.
Fix:
Once you have located the blank space, go back to the code in the editor and delete the blank space. Similarly, check and delete other blank spaces indicated in error and render the email once again.
Q: How do I solve an error stating None has no attribute value?
Explanation:
When an error occurs with a message similar to None/None type has no attribute/None has no attribute value, as displayed in the image above, it means that an empty object has been accessed. This can happen in a scenario where the code contains a variable that is expected to be added with an object (such as an item for a catalog based on its ID), but the code fails to do so.
For example, providing a non-existent item_id which causes the item_by_id function to fail to fetch any item from the catalog. When working with the same object later, rendering fails as the object does not exist.
Fix:
When encountering such an error, it is recommended to add a condition that checks whether the object exists or not. This practice can avoid unnecessary rendering issues.
Conditions are added to the code in the above image (see the image below). This condition prevents incorrect access to the object in case it does not exist. The {% if item%} results in True if there is a value assigned to the variable. If the result is False, the execution of the code within is not executed if the code calls for the given object.
Q: How can I target a customer property that has a space in it with Jinja?
Explanation:
Normally, the Jinja for targeting a customer property would look like this: {{ customer.property }}
However, if you would try to use the same syntax on the property that has a space in it, for example, {{ customer.my property }} it will result in an error as Jinja variables cannot contain one:
Fix:
In order to target such properties, you need to use different syntax, enwrapping the property in quotes and putting it into the square brackets: {{ customer[“my property”] }}
Q: How to create a bi-weekly condition using Jinja?
Explanation:
You can use the following jinja to check if the week is odd or even in your campaigns. It will allow to skip or alter sending depending on the week number.
Solution:
{% set week = time | from_timestamp('%W') %}
{% set remainder = week %2 %}
{% if remainder == 0 %}
True
{% else %}
False
{% endif %}