Twig variables, functions, and filters
Reference of the variables, functions, and filters you can use in Twig in Productsup rule boxes.
Besides the attributes of your data, you can use variables, functions, and filters in a Twig. This page lists the ones that are specific to Productsup and the standard Twig filters that work differently in the platform. For the basics of writing Twig in a rule box, see Twig-based rule box capabilities to enhance your product data.
Refer to the site, its tags, or the current attribute with Twig
Some sites share elements, such as a shared Dataflow. To distinguish data among sites, you can add the site ID or a site tag to your data with Twig. See Edit site settings to learn how to add a tag to a site.
You can use the following variables in a Twig:
| Variable | What it returns | Example |
|---|---|---|
currentValue | The value of the attribute where you add the rule box | {{ currentValue }} |
currentField | The name of the attribute where you add the rule box | {{ currentField }} |
Site.id | The ID of the site | {{ Site.id }} |
Tags | The site tags. Add the tag key after a dot | {{ Tags.your_tag_key }} |
ProjectTags | The tags of the project the site belongs to | {{ ProjectTags.your_tag_key }} |
AccountTags | The tags of the account the site belongs to | {{ AccountTags.your_tag_key }} |
CurrentSite.id | The ID of the site that runs the rule box, also when the site uses a shared Dataflow | {{ CurrentSite.id }} |
PID | The ID of the current site run | {{ PID }} |
If your site uses a shared Dataflow, Site refers to the site that shares the Dataflow, not to your site. To get the ID of the site that runs the rule box, use {{ CurrentSite.id }}.
Twig functions and filters you can use in Productsup
The platform runs Twig 3, so you can use the tags, filters, functions, and tests described in the Twig 3 documentation, with the exceptions described in this section. Productsup also adds the following functions:
| Function | What it does | Example |
|---|---|---|
fromField() | Returns the value of an attribute whose name contains spaces or special characters | {{ fromField('Long description') }} |
json_decode() | Turns JSON text into a list or object that you can go through with a for loop. Returns nothing if the value is not valid JSON | {% set data = json_decode(currentValue) %} |
json_encode() | Turns a list or object into JSON text | {{ json_encode(data) }} |
To read values from JSON, check that the decoded value is a list or object before you use it. For example, if the current value is [{"name": "red"}, {"name": "blue"}], the following Twig returns red, blue:
{%- set data = json_decode(currentValue) -%}
{%- if data is iterable -%}
{%- set names = [] -%}
{%- for item in data -%}
{%- set names = names|merge([item.name]) -%}
{%- endfor -%}
{{- names|join(', ') -}}
{%- endif -%}Filters that work differently in Productsup
For security reasons, the following Twig filters work differently in the platform than described in the Twig documentation:
| Filter | Behavior in Productsup |
|---|---|
filter | Ignores the arrow function you pass to it and removes all empty values and values of 0 from the list |
map | Returns the list unchanged |
reduce | Returns the list unchanged |
sort | Sorts the values in ascending order and ignores the arrow function you pass to it |
To change, select, or combine the items of a list, use a for loop instead. See Work with lists of values in Twig.
The spaceless filter and the apply spaceless tag are deprecated in the platform's Twig version, and using them can leave values empty. To remove whitespace, use whitespace control instead. See Line breaks and spaces in the template appear in the output.
Work with lists of values in Twig
Some attributes contain several values in one cell, such as a comma-separated list of colors. To process each value, split the value into a list, go through the list with a for loop, and join the result again.
For example, the following Twig removes empty entries from a comma-separated list and capitalizes each color. If the current value is red, ,blue, the result is Red, Blue:
{%- set result = [] -%}
{%- for item in currentValue|split(',') -%}
{%- set clean = item|trim -%}
{%- if clean is not empty -%}
{%- set result = result|merge([clean|capitalize]) -%}
{%- endif -%}
{%- endfor -%}
{{- result|join(', ') -}}A variable that you set for the first time inside a for loop is not available after the loop. To use a value after the loop, set the variable before the loop, as result in the example, and change it inside the loop.
In the Text Template With List rule box, ListValues contains the values of the list you select in the rule box. For example, {{ ListValues|join(', ') }} outputs all values of the list separated by a comma. See Text Template With List to learn how to set up the rule box.
How is this guide?