You can use functions to add complex logic to values rendered in your templates. Functions are tools for formatting, math operations, and manipulating lists and strings.
In this article, you’ll find a list of all the functions you can use in DOCX templates, along with some examples to help you understand how each one works.
List of functions
It formats a tag value. You can use it with or without parameters:
format
- if encountered on date value it will format it as short date string.
format(val)
- formats current value using specified format string. For example, you can use N2
for a numbers with two decimals.
format(val, locale)
- formats current value using specified format string and Locale. For example, you can use format(C, fr-FR)
to apply a specific currency to the tag.
It uses standard format strings and supports different Locales. For example, you can place tags with different locales in a docx template
{{num1|format(C, en-US)}}
{{num2|format(C, fr-FR)}}
Please find more information about using Locale in the Microsoft documentation:
Note
Special characters and spaces are supported, but you’ll need to enclose the tag in square brakes inside the curly braces:
{{[date value]|format(dd.MM.yyyy)}}
Template |
Data |
Result |
---|---|---|
Date: {{date|format(dd.MM.yyyy)}}
Date: {{[date value]|format(dd.MM.yyyy)}}
Date: {{date|format(MM/dd)}}
Date: {{date|format(U)}}
Time: {{date|format(hh:mm)}}
Number: {{num|format(C)}}
Number: {{[num+1]|format(C)}}
Number: {{num|format(C, fr-FR)}}
Number: {{num|format(P)}}
Number: {{num|format(N2)}}
|
{
"date": "2012-04-21T18:25:43-05:00",
"date value": "2021-05-25T10:44:00-02:00",
"num": 8,
"num+1": 5
}
|
Date: 21.04.2012
Date: 25.05.2021
Date: 04/22
Date: Saturday, April 21, 2012 11:25:43 PM
Time: 11:25
Number: $8.00
Number: $5.00
Number: 8.00 €
Number: 800.00%
Number: 8.00
|
This function returns an array of objects that meet a condition and assign them to a calculated property.
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.
This function reorders the items in a list and saves the sorted values in a new calculated property.
Syntax:
{{sorted = items|sort(property, DESC)}}
Sorting parameters
ASC
(Ascending): Sorts from A to Z or smallest to largest. This is the default and can be skipped.
DESC
(Descending): Sorts from Z to A or largest to smallest.
Template |
Data |
Result |
---|---|---|
Items: {{sorted = items|sort()}}
- {{sorted}}
|
{
"items": [987, 6534, 54]
}
|
Items:
- 54
- 987
- 6534
|
Employees: {{sorted = employees|sort(lastName, DESC)}}
- {{sorted.lastName}}, {{sorted.name}}
|
{
"employees": [
{
"name": "James",
"lastName": "Williams"
},
{
"name": "Jessica",
"lastName": "Brown"
},
{
"name": "Robert",
"lastName": "Jones"
},
{
"name": "Emily",
"lastName": "Davis"
}
]
}
|
Employees:
- Williams, James
- Jones, Robert
- Davis, Emily
- Brown, Jessica
|
map
accepts an unlimited number of values and maps a specific value to a specific output.
For example, we have some property that stores delivery types and has values:
EmailShipping
ElectronicalShipping
And we want to display them in a readable format like so:
Email shipping
Electronic shipping
Template |
Data |
Result |
---|---|---|
{{deliveryType|map(“EmailShipping” => “Email shipping”, “ElectronicShipping” => “Electronic shipping”)}}
|
{
"deliveryType": "EmailShipping"
}
|
Email shipping
|
Note
All values in double quotes are recognised as strings. If you want to map numbers, you don’t need to use double quotes in the condition.
For example, {{tag|map("Yes" => 1, "No" => 0)}}
.
regex(pattern, replacement)
- returns a new string with all matches of a RegExp pattern
replaced by a replacement
.
Returns substring. You can use the function with one or two parameters:
substring(index)
- returns substring of provided values after index
chars
substring(index,length)
- returns substring of provided values after index
with length
.
sum()
- calculates the sum of values in an array.
{{items|sum()}}
— sums the raw values in the array (e.g., [1, 2, 3]
becomes 6
),
{{items|sum(propertyName)}}
— sums the specified property (propertyName
) across all objects in the array.
Note
If you need to sum a calculated value like @value.quantity * @value.price
, assign the expression to a calculated property first, then use that property with sum
. See the example below.
Template |
Data |
Result |
---|---|---|
Total:
{{items|sum()}}
|
{
"items": [3, 2, 7]
}
|
Total:
12
|
{{sales.totalAmount = @value.quantity * @value.price}}
Total amount:
${{sales|sum(totalAmount)}}
Total quantity:
{{sales|sum(quantity)}}
|
{
"sales": [
{
"price": 3,
"quantity": 90
},
{
"price": 4,
"quantity": 60
},
{
"price": 5,
"quantity": 30
}
]
}
|
Total amount:
$660
Total quantity:
180
|
avg()
- calculates the average of values in an array.
{{items|avg()}}
— averages raw values in a simple array (e.g., [3, 2, 7]
→ 4
),
{{items|avg(propertyName)}}
— averages the specified property (propertyName
) across all objects in the array.
Note
If you need to average a calculated value like @value.quantity * @value.price
, assign the expression to a calculated property first, then use that property with avg
. See the example below.
Template |
Data |
Result |
---|---|---|
Average:
{{items|avg()}}
|
{
"items": [3, 2, 7]
}
|
Average:
4
|
{{sales.amount = @value.quantity * @value.price}}
Average amount:
${{sales|avg(amount)}}
Average quantity:
{{sales|avg(quantity)}}
|
{
"sales": [
{
"price": 3,
"quantity": 90
},
{
"price": 4,
"quantity": 60
},
{
"price": 5,
"quantity": 30
}
]
}
|
Average amount:
$220
Average quantity:
60
|
count()
- counts the number of items in an array.
join(separator, order)
- joins array values with a separator
(required) and sorts them in a specific order
(optional).
Note
To use quotes "
escape them with a backslash:
{{obj|join(\",\", ASC)}}
Template |
Data |
Result |
---|---|---|
{{arr|join(\", \")}}
{{arr|join(\", \", ASC)}}
{{arr|join(\"-\", DESC)}}
{{arr|join(; )}}
{{arr|join( : )}}
|
{
"arr": [1, 2, 3]
}
|
1, 2, 3
1, 2, 3
3-2-1
1;2;3
1:2:3
|
{{years = reports.year}}
{{years|join(\", \")}}
{{years|join(\", \", ASC)}}
{{years|join(\", \", DESC)}}
|
{
"reports": [
{
"year": "1992"
},
{
"year": "1987"
},
{
"year": "2005"
}
]
}
|
1987, 1992, 2005
1987, 1992, 2005
2005, 1992, 1987
|
offset(d)
- date and time value will be offset by d
days.
offset(d.hh:mm:ss)
- advanced approach for offsetting d
days, hh
hours, mm
minutes, ss
seconds.
Just replace d
, hh
, mm
and ss
by the required number of days, hours, minutes and seconds in this string pattern d.hh:mm:ss
.
Template |
Data |
Result |
---|---|---|
Without offset:
{{date}}
Plus 10 days
{{date|offset(10)}}
Minus 10 days:
{{date|offset(-10)}}
Plus 10 days, 1 hour,
5 minutes, 10 seconds:
{{date|offset(10.1:5:10)}}
Minus 10 days, 1 hour,
5 minutes, 10 seconds:
{{date|offset(-10.1:5:10)}}
|
{
"date": "2012-04-21T18:25:43-05:00"
}
|
Without offset:
4/22/2012 3:25:43 AM
Plus 10 days
5/2/2012 3:25:43 AM
Minus 10 days:
4/12/2012 3:25:43 AM
Plus 10 days, 1 hour,
5 minutes, 10 seconds:
5/2/2012 4:30:53 AM
Minus 10 days, 1 hour,
5 minutes, 10 seconds:
4/12/2012 2:20:33 AM
|
bool(yes, no, maybe)
- boolean value will be converted to yes
, no
or maybe
. You can specify your own value for each state. The last parameter is optional. You can use it if your bool value can be null.
repair-url
- this function corrects an URL to full qualified with HTTP scheme or checks correctness when a scheme is existing. When result URL is not correct - it removes it from the document.
Template |
Data |
Result |
---|---|---|
{{value|repair-url}}
|
{
"value": "picturesite.com/pics/picture.png"
}
{
"value": "google.com"
}
{
"value": ".net"
}
|
https://picturesite.com/pics/picture.png
https://google.com
|
The keep-token
function keeps tokens as they are.
It can be useful in case you have other system tags in double curly brackets (for instance, Adobe Sign text tags).
Or you have some text enclosed with double curly brackets as a part of a document.
Note
Special characters and spaces are supported, but you’ll need to enclose the tag in square brakes inside the curly braces:
{{[Sig es :+signer+:signature]|keep-token}}
Template |
Result |
---|---|
{{value|keep-token}}
{{Sig_es_:signer1:signature|keep-token}}
{{[Sig es :+signer+:signature]|keep-token}}
|
{{value}}
{{Sig_es_:signer1:signature}}
{{Sig es :+signer+:signature}}
|
The titleCase
function capitalizes the first letter of each word in a string (title case).
Template |
Data |
Result |
---|---|---|
{{text|titleCase}}
|
{
"text":"test string"
}
|
Test String
|