Skip to main content

Twig-based rule box capabilities to enhance your product data

An explanation of using Twig in Productsup to enhance product data.

Introduction

Twig is a template engine based on PHP that lets you add dynamic content to the template.

You can transform your data with the help of Twig in the rule boxes such as Text Template, Text Template with Lists, and Math Template in Data View or Dataflow.

Twig lets you make text transformations, do math calculations, dynamically update URLs, and apply simple conditional logic.

This document shows some examples of using Twig to optimize your product data.

Use rule boxes with Twig to edit the text and do calculations

You can enhance your data with the help of rule boxes that can use Twig to dynamically change the content of the attributes in Data View or Dataflow. To add a rule box, take the following steps:

  1. Go to Data View from the site's main menu.

  2. Choose the needed export channel or the intermediate stage in the drop-down menu on the left.

  3. Select Edit in the attribute column where you want to apply the rule box:

  4. Enter your text with Twig to integrate attributes' values in your text. See Add Twig in the rule boxes.

  5. Select Save.

Add Twig in the rule boxes

To get the needed result with Twig, create the Twig code according to the rules:

  1. The Twig should begin and end with curly brackets and spaces such as {{ text }}.

  2. The contents of Twig are case-sensitive meaning that if you refer to an attribute with a Twig, you need to enter the attribute's name exactly as it is in the feed. For example, {{ Long_description }} or {{ long_description }}.

  3. To get values from certain attributes and insert them into the template of the rule box with Twig, use the necessary Twig syntax:

    • If the attribute name consists only of alphanumeric characters and underscores, you can refer to it using this syntax: {{ attribute_name }}.

    • If the attribute name contains spaces, dots, commas, or other special characters, you can refer to it using this syntax: {{ fromField('attribute name')|raw }}.

    Tip

    Alternatively, you can rename such an attribute using only alphanumeric characters and underscores. Then, you can refer to the renamed attribute in a Twig using the standard syntax {{ attribute_name }}.

    If renaming an attribute isn't an option, you can copy it, rename the copy, and use the copy in a Twig.

  4. To edit the attribute's current value, use currentValue:

    • If the value consists only of alphanumeric characters and underscores, you can refer to it using this syntax: {{ currentValue }}.

    • If the value contains spaces, dots, commas, or other special characters, you can refer to it using this syntax: {{ fromField('currentValue')|raw }}.

Tip

Learn more about rule boxes with Twig by taking a video course Optimization: Rule boxes for platform.productsup.com on our Academy website.

Use AI-generator for Twig suggestions

If you need a suggestion for a possible Twig, use our AI-based Twig generator.

Watch this short video to have a better idea of how you can use the Twig generator in rule boxes:

  1. Select the >_ icon in a rule box.

    Text Template with Twig
  2. Describe the aim and result you want to achieve in the Twig generator window and select Generate.

    Twig generator window
  3. Select Copy below the Answer field.

  4. Paste the copied answer into the Twig field of the rule box.

Tip

To create the most accurate prompts, see our best practices in Create the right prompt for the AI Twig generator.

Refer to a site with Twig

Some sites use shared elements. To distinguish data among the sites, it is possible to add a site tag or site ID in the data feed. See Edit site settings to learn how to add a tag to a site.

If you need to refer to a site in your data feed, you can use Twig for site ID such as {{Tag.SiteID}} or a site tag such as {{Site.Tags.XXX}}, where XXX is the tag key that you can set in the site settings.

Refer to site tag

Leave a comment for Twig

It is helpful to add a comment to your Twig in the rule boxes to keep a reminder for your colleagues and yourself of the purpose or logic of the change. To leave a comment for a Twig, use the format {# … #} and enter the necessary text inside the brackets. For example, {# it's a comment to remind you what this Twig edits #}.

The comments are invisible in the data feed and you can only see them when you open the rule box.

Use cases for data editing with Twig

Here are some use cases where you can use Twig:

Use case

Attribute value

Text Template with Twig

Result

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

product_type: T-shirt

brand: Nike

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

This is a T-shirt from Nike.

Create a URL: In the link attribute, create a URL that includes brand, and category.

brand: Nike

category: T-shirts

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

https://www.store-example.com/Nike/T-shirts

Set value if empty: Change the current value to brand new if it is empty.

The current attribute is empty

{% if currentValue == '' %}{{'brand new'}}{% else %}{{currentValue}}{% endif %}

brand new

Prepend the text: Prepend the text 'cheap' if the price is lower than 130.

The current attribute: red jacket

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

Cheap red jacket