Productsup
Twig in rule boxes

Twig examples for common data transformations

Twig examples for the Text Template rule box, such as setting values if empty, calculating discounts, and cleaning up lists.

7 min read

The following examples use the Text Template rule box. Replace the attribute names and values with your own. For the basics of writing Twig in a rule box, see Twig-based rule box capabilities to enhance your product data. For examples of if/else logic, see Use conditional logic in Twig. The hyphens in tags such as {%- and -%} let you write a Twig on several lines without adding line breaks to the value. See Line breaks and spaces in the template appear in the output.

Create a text from several attributes

Create simple product descriptions in the description attribute using the values from product_type and brand:

This is a {{ product_type }} from {{ brand }}.

For example, if product_type is T-shirt and brand is Alpha, the result is This is a T-shirt from Alpha.

Create a URL from attribute values

Create a URL in the link attribute that includes the values from brand and category:

https://www.store-example.com/{{ brand }}/{{ category }}

For example, if brand is Alpha and category is T-shirts, the result is https://www.store-example.com/Alpha/T-shirts.

Set a value if the attribute is empty

Change the current value to brand new if it is empty, and keep it otherwise:

{%- if currentValue|trim is empty -%}
brand new
{%- else -%}
{{ currentValue }}
{%- endif -%}

For example, if the current value is empty, the result is brand new.

Prepend text based on the price

Add the text Cheap in front of the current value if price is lower than 130:

{%- if price < 130 -%}
Cheap {{ currentValue }}
{%- else -%}
{{ currentValue }}
{%- endif -%}

For example, if the current value is red jacket and price is 99, the result is Cheap red jacket.

Append a value only if the current value is not empty

Add the currency from price_currency to the sale price, but leave products without a sale price empty:

{%- set value = currentValue|trim -%}
{%- if value is not empty -%}
{{ value }} {{ price_currency }}
{%- endif -%}

For example, if the current value is 49.99 and price_currency is EUR, the result is 49.99 EUR.

Remove a value that matches another attribute

Empty the sale price if it is the same as price:

{%- if price|trim != currentValue|trim -%}
{{ currentValue }}
{%- endif -%}

For example, if the current value is 20.00 and price is 20.00, the attribute is empty.

Set a value based on another attribute

Set an emissions class based on the values in fuel_type, grouping several values that lead to the same result:

{%- if fuel_type == 'electric' -%}
zero_emissions
{%- elseif fuel_type in ['diesel', 'gasoline', 'hybrid'] -%}
euro6e
{%- else -%}
{{ currentValue }}
{%- endif -%}

For example, if fuel_type is diesel, the result is euro6e.

Calculate a discount percentage from prices with a currency code

Remove the currency code from price and sale_price, then calculate the discount as a whole number:

{%- set p = price|replace({'EUR': '', ' ': ''})|trim -%}
{%- set s = sale_price|replace({'EUR': '', ' ': ''})|trim -%}
{%- if p > 0 -%}
{{ (((p - s) / p) * 100)|round(0) }}
{%- endif -%}

For example, if price is 100.00 EUR and sale_price is 80.00 EUR, the result is 20. Replace EUR with the currency code in your data.

The Discount Percentage rule box needs numeric values in both price attributes. If your prices contain a currency code, either remove it in an earlier stage, for example, with the Make Valid Price rule box, or use the Twig from this section instead of the rule box. See Work with prices & math for more information about the Discount Percentage rule box.

Clean up a list of values

Remove the quotation marks around each entry in a quoted, comma-separated list and drop the entries longer than 30 characters:

{%- set result = [] -%}
{%- for item in currentValue|split('","') -%}
  {%- set clean = item|trim('"')|trim -%}
  {%- if clean|length > 0 and clean|length <= 30 -%}
    {%- set result = result|merge([clean]) -%}
  {%- endif -%}
{%- endfor -%}
{{- result|join(', ') -}}

For example, if the current value is "navy blue","crew neck","Normal wear may reduce the shine of the surface.", the result is navy blue, crew neck.

Keep only future dates from an Excel file

Convert an Excel date, which the platform imports as a serial number, into a readable date, and leave the value empty if the date is today or in the past:

{%- if currentValue matches '/^\\d+$/' -%}
  {%- set ts = (currentValue - 25569) * 86400 -%}
  {%- if ts|date('Y-m-d') > 'now'|date('Y-m-d') -%}
    {{- ts|date('Y-m-d') -}}
  {%- endif -%}
{%- endif -%}

For example, if the current value is 47484, the result is 2030-01-01.

Tip

To exclude the products whose value is empty after this Twig, add the Skip Row If Empty rule box after the Text Template rule box on the same attribute. See Skip Row If Empty to learn how to set up the rule box.

On this page

Still stuck?

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

Contact support