PDFMonkey filters
Array filters
push
Adds an item to an array
{% assign arr = "earth wind" | split: " " %}
{% assign arr = arr | push: "fire" %}
{{arr | to_sentence}}
earth, wind and fire
slice_by
Slices an array in groups of N elements.
{% assign reindeers = "Dasher Dancer Prancer Vixen Comet Cupid Donner Blitzen Rudolph" | split: " " %}
{% assign reindeerPairs = reindeers | slice_by: 2 %}
{% for reindeerPair in reindeerPairs %}
Pair:
{% for reindeer in reindeerPair %}
- {{reindeer}}
{% endfor %}
{% endfor %}
Pair:
- Dasher
- Dancer
Pair:
- Prancer
- Vixen
Pair:
- Comet
- Cupid
Pair:
- Donner
- Blitzen
Pair:
- Rudolph
sum
Returns the sum of a numbers array.
Let's say you have this in your data:
{
"array": [1, 2, 3, 4, 5]
}
{{array | sum}}
15
to_sentence
Converts the array to a comma-separated sentence where the last element is joined by the connector word.
{% assign arr = "earth wind" | split: " " %}
{% assign arr = arr | push: "fire" %}
{{arr | to_sentence}}
{{arr | to_sentence: ' - '}}
{{arr | to_sentence: ' - ', 'and finally '}}cod
earth, wind and fire
earth - wind and fire
earth - wind and finally fire
where_exp
Similar to Liquid's where but accepts an expression.
Let's start with the data:
{
"products": [
{ "name": "Product 1", "price": 1.00 },
{ "name": "Product 2", "price": 2.00 },
{ "name": "Product 3", "price": 3.00 },
{ "name": "Product 4", "price": 2.00 },
{ "name": "Product 5", "price": 1.00 }
]
}
{% assign cheapProducts = products | where_exp: "prod", "prod.price < 1" %}
{% for product in cheapProducts %}
- {{product.name}}
{% endfor %}
- Product 1
- Product 5
Date and time filters
in_time_zone
{{"2050-01-02 12:34:56" | in_time_zone: "Pacific/Galapagos" | date: "%Y-%m-%d %H:%M"}}
2050-01-02 06:34
URL filters
ensure_protocol
You might get asset URLs that don’t include any protocol. Sadly our rendering engine doesn’t support this type of URL. For this reason we provide ensure_protocol
that will add a protocol as needed:
{{"//example.com/some-picture.jpg" | ensure_protocol}}
{{"//example.com/some-picture.jpg" | ensure_protocol: "http"}}
{{"http://example.com/some-picture.jpg" | ensure_protocol}}
https://example.com/some-picture.jpg
http://example.com/some-picture.jpg
http://example.com/some-picture.jpg
Number filters
format
The format
filter is useful for advanced number formatting. For simpler formatting, prefer with_delimiter.
{{5 | format: "%05d"}} -> 00005
{{5 | format: "%08.3f"}} -> 0005.000
{{5 | format: "%.2f"}} -> 5.00
{{5.1234 | format: "%.2f"}} -> 5.12
00005
0005.000
5.00
5.12
with_delimiter
For basic number formatting like money for instance, the with_delimiter
filter will help you set a thousand delimiter, a decimal separator and the precision.
{{12345678 | with_delimiter}} 12,345,678
{{'123456' | with_delimiter}} 123,456
{{1234.567 | with_delimiter}} 1,234.567
{{'12345a' | with_delimiter}} 12345a
{{12345678 | with_delimiter, delimiter: " "}} 12 345 678
{{1234.567 | with_delimiter, delimiter: " "}} 1 234.567
{{1234.567 | with_delimiter, separator: " "}} 12,345,678 05
{{12345678 | with_delimiter, precision: 2}} 12,345,678.00
{{12345678 | with_delimiter, precision: 2, strip_insignificant_zeros: true }} 12,345,678
{{1234.567 | with_delimiter, precision: 2, strip_insignificant_zeros: true }} 1,234.57
{{1234.567 | with_delimiter,
delimiter: " ",
separator: ",",
precision: 2,
strip_insignificant_zeros: true
}}
1 234,57
{{1234.003 | with_delimiter,
delimiter: " ",
separator: ",",
precision: 2,
strip_insignificant_zeros: true
}}
1 234
HTML filters
entities
This filter can be very useful if you have to deal with accented or special characters inside the header or footer. It will convert any non-latin character to its HTML entity.
{{"Marie Skłodowska Curie" | entities}}
Marie Skłodowska Curie
JSON filters
json
If you need to insert dynamic data in its original JSON format (like we do for charts for instance) the json
filter is what you need. Notice it’s returning a string, not the object itself.
{{'banana, apple' | split: ', ' | json}}
["banana", "apple"]
parse_json
If you have a value in your payload that contains a JSON string, you can use the parse_json
filter to parse it and use it as additional data.
{% assign obj = '{ "key": "value" }' | parse_json %}
{{obj.key}}
value
Last updated
Was this helpful?