PDFMonkey filters

Array filters

push

Adds an item to an array

Input
{% assign arr = "earth wind" | split: " " %}
{% assign arr = arr | push: "fire" %}

{{arr | to_sentence}}
Output
earth, wind and fire

slice_by

Slices an array in groups of N elements.

Input
code{% 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 %}
Output
  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:

JSON
{
  "array": [1, 2, 3, 4, 5]
}
Input
{{array | sum}}
Output
15

to_sentence

Converts the array to a comma-separated sentence where the last element is joined by the connector word.

Input
{% 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:

JSON
{
  "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 }
  ]
}
Input
{% 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

Input
{{"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:

Input
{{"//example.com/some-picture.jpg" | ensure_protocol}}
{{"//example.com/some-picture.jpg" | ensure_protocol: "http"}}
{{"http://example.com/some-picture.jpg" | ensure_protocol}}
Output
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.

Input
{{5 | format: "%05d"}}      -> 00005
{{5 | format: "%08.3f"}}    -> 0005.000
{{5 | format: "%.2f"}}      -> 5.00
{{5.1234 | format: "%.2f"}} -> 5.12
Output
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.

Input
{{12345678 | with_delimiter}}
{{'123456' | with_delimiter}}
{{12345678.05 | with_delimiter}}
{{12345678 | with_delimiter, delimiter: '.'}}
{{12345678 | with_delimiter, delimiter: ','}}
{{12345678 | with_delimiter, delimiter: ',', precision: 2}}
{{12345678.05 | with_delimiter, separator: ' '}}
{{12345678.05 | with_delimiter, delimiter: ' ', separator: ','}}
{{'123a' | with_delimiter}}
Output
12,345,678
123,456
12,345,678.05
12.345.678
12,345,678
12,345,678.00
12,345,678 05
12 345 678,05
123a

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.

Input
{{"Marie Skłodowska Curie" | entities}}
Output
Marie Sk&lstrok;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.

Input
{{'banana, apple' | split: ', ' | json}}
Output
["banana", "apple"]

Last updated