Productsup
Twig in rule boxes

Use conditional logic in Twig

Assign values based on conditions with if/else logic in Twig, and choose between Twig and rule box conditions.

9 min read

With Twig, you can assign values based on conditions, for example, to set custom labels, choose between values from several attributes, or group products. For simple conditions that existing rule boxes can handle, rule box conditions can be the better choice. See Choose between Twig and rule box conditions.

For the basics of writing Twig in a rule box, see Twig-based rule box capabilities to enhance your product data.

Assign values with conditional logic in Twig

To assign a value based on conditions, such as setting a custom label depending on stock, price, and brand, write if/else logic in a Text Template rule box. Twig checks each condition in order and outputs the value of the first condition that is true.

For example, the following Twig in a custom_label_0 attribute in your export sorts products into four groups:

{#- Replace the attribute names and values with your own -#}
{%- if availability == 'out of stock' -%}
out_of_stock
{%- elseif sale_price is not empty and sale_price < price -%}
on_sale
{%- elseif brand in ['Alpha', 'Beta', 'Gamma'] -%}
top_brand
{%- else -%}
standard
{%- endif -%}
availabilitypricesale_pricebrandcustom_label_0 (result)
out of stock50.0040.00Alphaout_of_stock
in stock50.0040.00Alphaon_sale
in stock50.00Betatop_brand
in stock50.00Deltastandard

The first product is on sale and from a top brand, but it gets out_of_stock because the platform stops at the first condition that is true. Put the most important conditions first.

The hyphens in the tags remove the line breaks between the lines, so the value contains only the label. See Line breaks and spaces in the template appear in the output. If your prices contain a currency code, remove it before you compare the prices. See Errors while the Twig runs make the value empty.

Operators for conditions in Twig

You can use the following operators in your conditions:

OperatorWhat it checksExample
==, !=The values are equal or not equal. The comparison is case-sensitive{% if color == 'red' %}
<, >, <=, >=A number is smaller or greater than another number{% if price >= 100 %}
in, not inA value is in a list of values, or a text is part of another text{% if brand in ['Alpha', 'Gamma'] %}
starts with, ends withA text starts or ends with another text{% if gtin starts with '400' %}
matchesA text matches a regular expression{% if title matches '/organic/i' %}
is empty, is not emptyA value is empty or not empty{% if sale_price is not empty %}
and, or, notCombines or negates conditions. Use parentheses to group conditions{% if (color == 'red' or color == 'blue') and stock > 0 %}

To compare text without considering upper and lower case, apply the lower filter to both sides, for example, {% if brand|lower == 'alpha' %}.

Split complex logic into a helper attribute

If a condition gets long, or you need the same check in several export attributes, calculate it once in a helper attribute in the intermediate stage. Because a Twig reads other attributes from the previous stage, a Twig in your export can then use the result of the helper attribute. See Which attribute values a Twig reads.

For example, you want to export the sale price only if the recommended retail price in rrp is not lower than price:

  1. Add a new attribute, for example, rrp_below_price, to the intermediate stage. Add a Text Template rule box to it with the following Twig: {% if rrp is not empty and rrp < price %}true{% endif %}.
  2. In your export, add a Text Template rule box to sale_price with the following Twig: {% if rrp_below_price != 'true' %}{{ currentValue }}{% endif %}.

You can check the values of the helper attribute in Data View, which makes the logic easier to test. As long as you do not connect the helper attribute to an attribute in your export, the platform does not export it.

Choose between Twig and rule box conditions

You can also build if/then/else logic without code with Rule Box Conditions. A rule box condition checks one or more condition statements, combined with AND and OR, and applies the THEN rule boxes if the statements are true and, optionally, the ELSE rule boxes if they are false. See Apply rule boxes based on conditions to learn how to set up a rule box condition.

Use rule box conditions when:

  • The logic has two outcomes that existing rule boxes can produce, for example, a Static Value rule box if the condition is true and another one if it is false.
  • Colleagues who do not write code need to understand or change the logic, as the conditions appear in a visual editor.
  • You want to check values against an Inclusion/Exclusion list.

Use Twig when:

  • You need more than two outcomes in one attribute, such as several elseif branches.
  • The result combines values from several attributes or calculates a value.
  • You need to change values before you compare them, for example, to remove a currency code or ignore upper and lower case.

You can also combine both. Add a Text Template rule box to the THEN or ELSE rule boxes of a rule box condition, so the condition decides when the platform applies the Twig and the Twig decides what the value is.

On this page

Still stuck?

Reach out to our support team and we’ll help you get unstuck.

Contact support