Array filters
push
Adds an item to an array
Copy {% assign arr = "earth wind" | split: " " %}
{% assign arr = arr | push: "fire" %}
{{arr | to_sentence}}
slice_by
Slices an array in groups of N elements.
Copy {% 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 %}
Copy 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:
Copy {
"array": [1, 2, 3, 4, 5]
}
to_sentence
Converts the array to a comma-separated sentence where the last element is joined by the connector word.
Copy {% assign arr = "earth wind" | split: " " %}
{% assign arr = arr | push: "fire" %}
{{arr | to_sentence}}
{{arr | to_sentence: ' - '}}
{{arr | to_sentence: ' - ', 'and finally '}}cod
Copy 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:
Copy {
"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 }
]
}
Copy {% assign cheapProducts = products | where_exp: "prod", "prod.price < 1" %}
{% for product in cheapProducts %}
- {{product.name}}
{% endfor %}
Copy - Product 1
- Product 5
Date and time filters
in_time_zone
Copy {{"2050-01-02 12:34:56" | in_time_zone: "Pacific/Galapagos" | date: "%Y-%m-%d %H:%M"}}
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:
Copy {{"//example.com/some-picture.jpg" | ensure_protocol}}
{{"//example.com/some-picture.jpg" | ensure_protocol: "http"}}
{{"http://example.com/some-picture.jpg" | ensure_protocol}}
Copy 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 .
Copy {{5 | format: "%05d"}} -> 00005
{{5 | format: "%08.3f"}} -> 0005.000
{{5 | format: "%.2f"}} -> 5.00
{{5.1234 | format: "%.2f"}} -> 5.12
Copy 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.
Copy {{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}}
Copy 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 .
Copy {{"Marie Skłodowska Curie" | entities}}
Copy 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.
Copy {{'banana, apple' | split: ', ' | json}}
Last updated 5 months ago