Skip to main content

Functions

A function is a block of code that performs a specific task. Functions usually take in data, process it, and return a result. You can use functions inside dynamic contents to perform an action in Swarm. The following are functions that can be used in dynamic contents.

1. default()

The default() function returns a default value if the specified data returns no value.

Syntax:

{{default(data_name, ‘default_value’)}}

Parameters:

  • data_name: The data can be of any data type (task data, environment variables, data store items, or metadata)
  • default_value: The default value to be returned if the specified data returns no value

2. format()

The format() function formats the specified values and inserts them into a specific pattern.

Syntax:

{{format(value, {pattern})}}

Parameters:

  • value: Value or data to be formatted
  • pattern: Format pattern

These are some examples of format() functions that can be used to format currency.

  • {{format(123456.78, ${:,.2f})}} = $ 123,456.78 (":,": Use a comma as a thousand separator, ".2f": Round to 2 decimal places)
  • {{format(123456.78, €{:,.2f})}} = € 123,456.78 (The currency symbol is defined before the curly braces)
  • {{format(123456.78, ${:,.0f})}} = $ 123,456 (".0f": Round to zero decimal places)

3. strftime()

The strftime() function converts date objects to their string representation.

Syntax:

{{strftime(date_object, 'format_code')}}

Parameters:

  • date_object: Date object to be converted
  • format_code: Format code

The following are some format codes that can be used in this function.

Format codeReturned date format
'%Y-%m-%d'yyyy-mm-dd
'%d-%m-%Y'dd-mm-yyyy
'%d-%m-%y'dd-mm-yy
'%d.%m.%Y'dd.mm.yyyy
'%d.%m.%y'dd.mm.yy
'%d/%m/%Y'dd/mm/yyyy
'%d/%m/%y'dd/mm/yy
'%d-%m/%y'dd-mm/yy
'%d-%m/%Y'dd-mm/yyyy
'%d-%m %y'dd-mm yy
'%d-%m %Y'dd-mm yyyy
'%m/%d/%Y'mm/dd/yyyy
'%m/%d/%y'mm/dd/yy
'%m.%d.%Y'mm.dd.yyyy
'%m.%d.%y'mm.dd.yy

4. exists()

The exists() function is used to check whether specific data exists or not. This function returns a True/False value.

Syntax:

{{exists(data_name)}}

Parameters:

  • data_name: Data to be checked (can be task data, environment variables, data store items, or metadata)