Articles on: Expression language

Common use-cases of the Expression language

Using filter and iterate function


Let's say you have an array of line items, where items can be of different type (e.g. "Food", "Drinks", etc...) and you need to get a SUM of a specific type. Then you can do it using a combination of filter and iterate function:
{% sum(iterate(filter({line_items}, 'type == "Drinks"'),'total_price')) %}

Example JSON dataset:
[
  {
    "line_items": [
      {
        "name": "Cola",
        "price": 2,
        "qty": 2,
        "total_price": 4,
        "type": "Drinks"
      },
      {
        "name": "Fanta",
        "price": 2,
        "qty": 1,
        "total_price": 2,
        "type": "Drinks"
      },
      {
        "name": "Burger",
        "price": 4,
        "qty": 2,
        "total_price": 8,
        "type": "Food"
      }
    ]
  }
]

The result of our expression would be 6.

Use "Number" formatting when creating calculations or using sum, average, sumproduct functions.

Working with sub-items in the Array/Objects


In expression language, the sub-items are accessed with brackets.

Example JSON dataset:
[
  {
    "cars": [
      {
        "name": "Ford",
        "model": {
          "name": "Mustang",
          "year": "1967"
        },
        "options": {
          "color": "Red"
        }
      },
      {
        "name": "Chevrolet",
        "model": {
          "name": "El Camino",
          "year": "1987"
        },
        "options": {
          "color": "Black"
        }
      }
    ]
  }
]

For example, you need to get the count of cars where a model name is "Mustang".
The expression would look like this:
{% count(filter({cars}, 'model["name"] == "Mustang"')) %}

The result in this case would be 1.

Using Join function to get values from an array in a Text component


In this example we have an array of cars with objects - options and model. We would like to get the car name, model name and color for each car from the array and get the output in one text component.

{% join(', ', iterate({cars}, 'name ~ " " ~ model["name"] ~ "(" ~ options["color"] ~ ")"')) %}

Example JSON dataset:
[
  {
    "cars": [
      {
        "name": "Ford",
        "model": {
          "name": "Mustang",
          "year": "1967"
        },
        "options": {
          "color": "Red"
        }
      },
      {
        "name": "Chevrolet",
        "model": {
          "name": "El Camino",
          "year": "1987"
        },
        "options": {
          "color": "Black"
        }
      }
    ]
  }
]


The output of this particular example would be: "Ford Mustang(Red), Chevrolet El Camino(Black)"

Changing the timezone of a date



Dates are usually pulled to the document editor with the original timezone from the e-commerce platform and you need to use the DateTime expression function with timezone to convert the times in the editor.{% datetime({dateValue}, 'UTC') %}
The expression function needs timezone in PHP format, which you can find here: https://www.php.net/manual/en/timezones.php

For example:

{% datetime({TestDateTime}, 'Europe/London') %}

Function str_replace



The str_replace([string: search], [string: replace], [string: the original string]) function replaces all occurrences of a substring within a string.

{% str_replace("dog", "cat", "this is dog") %}

This expression output is: this is cat

Tip: If you have a long string of e.g. options, where each option is separated with "/" and you would like to show each option on a separate line, you can use the function str_replace.

{% str_replace('/','<br>',{options_text}) %}

Updated on: 18/12/2022

Was this article helpful?

Share your feedback

Cancel

Thank you!