Operations in DOCX, XLSX and PPTX templates

Operations can be applied to the data source property that contains an array of objects. It allows processing the data before putting them into the template.

List of operations

filter

The operation returns an array of the objects that meet a condition and assign them to a token initiated with alias.

Syntax:

{{filteredObjects = objects|filter(value.objectProperty != false)}}

Available operators:

  • == equal to,

  • != not equal to,

  • > more than,

  • < less than,

  • >= more than or equal to,

  • <= less than or equal to,

  • || logical “or”,

  • && logical “and”,

  • () group.

Example

Template

Data

Result

{{filteredColors = colors|filter(value.name != “green”)}}

Filtered colors:

  • {{filteredColors.name}}

{
  "colors": [
    {
      "name": "red"
    },
    {
      "name": "green"
    },
    {
      "name": "blue"
    }
  ]
}

Filtered colors:

  • red

  • blue

sort

The operation sorts items in the array of objects and may be used either separately or with alias.

Syntax

Usage with the alias:

{{sorted = items|sort(property, DESC)}}

{{sorted.property}}

Separate usage:

{{items|sort(property).property}}

{{items|sort(property1, ASC).property2}}

Sorting parameters

  • ASC ascending order (applied by default, may be skipped),

  • DESC descending order.

Example

Template

Data

Result

{{sorted = customers|sort(lastName, ASC)}}

Customers:
- {{sorted.lastName}}, {{sorted.firstName}}
{
  "customers": [
    {
      "firstName": "Adele",
      "lastName": "Vance"
    },
    {
      "firstName": "Diego",
      "lastName": "Siciliani"
    },
    {
      "firstName": "Johanna",
      "lastName": "Lorenz"
    }
  ]
}
Customers:
- Lorenz, Johanna
- Siciliani, Diego
- Vance, Adele

top

The operation returns a specified number of the top objects from the array and assign them to a token initiated with alias.

Syntax:

{{topObjects = objects|top(3)}}

Example

Template

Data

Result

{{topColors = colors|top(2)}}

Top colors:

  • {{topColors.name}}

{
  "colors": [
    {
      "name": "red"
    },
    {
      "name": "green"
    },
    {
      "name": "blue"
    }
  ]
}

Top colors:

  • red

  • green

at

The operation returns a certain object from the array by its index and assign it to a token initiated with alias.

Syntax:

{{atObject = objects|at(2)}}

Example

Template

Data

Result

{{atColor = colors|at(1)}}

My favorite color is {{atColor.name}}.

{
  "colors": [
    {
      "name": "red"
    },
    {
      "name": "green"
    },
    {
      "name": "blue"
    }
  ]
}

My favorite color is green.

slice

The operation returns a portion of an array of objects from the start index to the end index (the object at the end index is not included). The array portion is then assigned to a token initiated with alias. Indexes are zero-based. The end index is optional.

Syntax:

{{slicedObjects = objects|slice(1, 3)}}

Available operators:

Example

Template

Data

Result

{{slicedNumbers = numbers|slice(1, 3)}}

Sliced numbers:

  • {{slicedNumbers.name}}

{
  "numbers": [
    {
      "name": "zero"
    },
    {
      "name": "one"
    },
    {
      "name": "two"
    },
    {
      "name": "three"
    },
    {
      "name": "four"
    }
  ]
}

Sliced numbers:

  • one

  • two

{{slicedNumbers = numbers|slice(2)}}

Sliced numbers:

  • {{slicedNumbers.name}}

{
  "numbers": [
    {
      "name": "zero"
    },
    {
      "name": "one"
    },
    {
      "name": "two"
    },
    {
      "name": "three"
    },
    {
      "name": "four"
    }
  ]
}

Sliced numbers:

  • two

  • three

  • four