Troubleshoot Twig in rule boxes
Fix common Twig problems in Productsup rule boxes, such as unexpected line breaks, empty values, and unchanged values.
If a rule box with Twig returns an unexpected, unchanged, or empty value, one of the following mistakes is often the cause. For the basics of writing Twig in a rule box, see Twig-based rule box capabilities to enhance your product data.
Line breaks and spaces in the template appear in the output
The rule box outputs everything outside of the Twig tags exactly as you enter it, including line breaks and spaces. If you split a template over several lines or indent it to make it easier to read, the line breaks and spaces can become part of the value. They are invisible in Data View, but they can make an analyzer test flag the value as invalid or prevent a value from being completely empty. Depending on the export format, the platform may remove them when it writes the export file, but they stay in the value in Data View and in the rule boxes that follow.
For example, this template outputs euro6e followed by a line break, so the value has one character more than expected. Twig removes the first line break after a tag, but it keeps the line break before {% endif %}:
{% if fuel_type == 'electric' %}
zero_emissions
{% else %}
euro6e
{% endif %}To avoid this, write the template on a single line:
{% if fuel_type == 'electric' %}zero_emissions{% else %}euro6e{% endif %}Alternatively, add a hyphen inside the tag delimiters, such as {%- and -%}, or {{- and -}}. The hyphen removes the whitespace and line breaks on that side of the tag, so you can keep the template on several lines:
{%- if fuel_type == 'electric' -%}
zero_emissions
{%- else -%}
euro6e
{%- endif -%}Invalid Twig leaves the value unchanged
If the Twig in a Text Template or Text Template With List rule box contains a syntax error, for example, a missing %} or an operator that Twig does not support, the platform skips the rule box. The attribute keeps the value that the rule box received: the result of the rule box before it, or, if the Twig rule box is the first one, the value of the connected attribute from the previous stage. For example, if link is connected to product_type, the URLs from link appear in product_type.
To make the attribute stay empty instead, add a Static Value rule box with an empty value before the Text Template rule box. See Static Value to learn how to set up the rule box.
In the Math Template rule box, a syntax error makes the value empty instead.
Errors while the Twig runs make the value empty
If the Twig is valid but fails while the platform processes a product, the rule box returns an empty value for that product. Common causes are:
- Calculations with values that are not plain numbers. Twig can only calculate with numbers that use a dot as the decimal separator and contain no other text. Values such as
23,99or100.00 EURmake the calculation fail. Remove the currency or change the decimal separator in an earlier stage, for example, with the Make Valid Price rule box, or within the Twig with thereplacefilter. See Calculate a discount percentage from prices with a currency code. - Division by zero. If the divisor is
0or empty, the result is empty. To avoid this, check the divisor first, for example,{% if price > 0 %}. - Deprecated filters, such as
spaceless.
Comparisons such as < or > do not fail with values like 100.00 EUR, but the platform compares them as text, which can give unexpected results. Remove any text from numbers before you compare them.
Double the backslashes in regular expressions
Twig removes single backslashes from text in quotation marks. If your regular expression contains a backslash, such as \d for a digit, write it with two backslashes:
{% if currentValue matches '/^\\d+$/' %}{{ currentValue }}{% endif %}Negate a matches condition with not and parentheses
Twig does not support not matches as an operator. To check that a value does not match a regular expression, put not in front of the whole condition and wrap the condition in parentheses:
{% if not (currentValue matches '/exclude/i') %}keep{% else %}remove{% endif %}Check for empty values with is empty
To check whether a value is empty, use is empty or is not empty, for example, {% if currentValue is empty %}. The test returns true for empty values and for attributes that do not exist in the previous stage. Comparing the value with '', as in {% if currentValue == '' %}, gives the same result.
Neither check treats values that contain only spaces as empty. To also treat these values as empty, apply the trim filter first, for example, {% if currentValue|trim is not empty %}.
How is this guide?