List Filters

List Filters

compact filter

Removes all null, empty, and invalid (is_valid == false, empty lists, etc..) objects from the list.

compact

Related

concat filter

Adds all of the items from the other list onto the end of the input list.

concat: list other

If either the input or other is not a list, it will be treated as a list with a single object. If either is null, it will be treated as an empty list.

Examples

Use the concat filter to join multiple lists together

Joining lists together

Copy
{% "a" | concat:"b" | json_encode %}
["a", "b"]

Use the list methods and filters to create and manage lists of data in your templates

Getting and manipulating entity lists

Copy
{%- datastore_items var featured_houses = datastore:"houses" query:"is_featured = true" -%}
{%- datastore_items var houses_by_folder = datastore:"houses" folder:entity.folder -%}

Concatenate and uniq the easy way

{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

{% var houses = featured_houses | concat: houses_by_folder %}

Unique manually

{% var houses = houses | uniq %}

Get one random item from the list

{% var random_house = houses | rand %}
Get a random item from the list. Prevents the page from being fast-cached, and every time the page is loaded a new random item will be chosen.

Get one random item from the list and allow fast-caching

{% var random_house = houses | rand:1, false, false %}
Get a random item from the list and allow the page to be fast-cached, resulting in much faster pageload speeds but the same random item will be used until the fast cache expires.

Sort the full list randomly

{% set houses = houses | shuffle %}
Sort the full list randomly. The page will not be able to be fast-cached, and every time the page is loaded a new random order will be chosen. To allow the page to be fast-cached, pass false to the shuffle command.

Various ways to filter and slice

{%- var one_rooms = houses | where: "rooms", "1" | sort: "price" -%}
{%- var cheapest = one_rooms | first -%}
{%- var mid_houses = one_rooms | slice: 1, 6 -%}
{%- var expensive_houses = one_rooms | slice: 7 -%}
{%- var num_expensive_houses = expensive_houses | size -%}

Advanced filter

{% var few_rooms = houses | where_exp: "house", "house.rooms.value < 3" %}

Grouping

{%- var grouped_by_rooms = houses | group_by: "rooms" | sort: "Key" -%}
{%- for list in grouped_by_rooms -%}
<p>{{list.Key | default: "Unknown"}} Rooms ({{list.Value | size }} houses)</p>
{%- endfor -%}

Mapping and Compact

{%- var mapped_by_rooms = grouped_by_rooms | map: "Value" -%}
{%- for list in mapped_by_rooms -%}
<h4>{{list[0].rooms | default: "Unknown"}} Rooms</h4>
<ul>
<li>{{list | map: "description" | compact | join: "</li><li>" }}</li>
</ul>
{%- endfor -%}

Related

first filter

Returns the first item in the input list

first

If the input is not a list, it will be returned without modification.

Examples

Use the first and last filter to get a specific item in a list. In some cases, you can use square bracket notation to get an item at a specific index.

Getting a specific item from a list

Copy
{% var input = 'a,b,c,d' | split:',' %}

first

{{input | first}}
a

last

{{input | last}}
d

square bracket

{{input[2]}}
c
[{{input[5]}}]
[]
{{input[-1]}}
d

Related

group_by filter

Groups the list by the given property and returns the results as a list of objects. Each object in the result set has a Key property which is the value that they are grouped by and a Value property which is the list of objects that have the matching Key property. Any objects in the list that do not have the given property will be in a result with a null Key.

group_by: String property

If the input is null it will return an empty list. If the input is not null and is not a list, it will be treated as a list with a single object.

Examples

Use the group_by filter to group objects by a specific property. For example, you can group a list of blog posts by blog.

Group blog posts by blog

Copy
{%- blog_posts posts = limit: 40 start: 1 sort_by: 'date_posted' sort_direction: 'desc' -%}
{%- var grouped_by_blog = posts | group_by: 'blog' -%}
{%- for group in grouped_by_blog -%}
<p>{{ group.Key.title }}: {{ group.Value | size }}</p>
{%- endfor -%}
<p>General Blog: 22</p>
<p>Announcements: 10</p>
<p>Events: 8</p>

Use the list methods and filters to create and manage lists of data in your templates

Getting and manipulating entity lists

Copy
{%- datastore_items var featured_houses = datastore:"houses" query:"is_featured = true" -%}
{%- datastore_items var houses_by_folder = datastore:"houses" folder:entity.folder -%}

Concatenate and uniq the easy way

{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

{% var houses = featured_houses | concat: houses_by_folder %}

Unique manually

{% var houses = houses | uniq %}

Get one random item from the list

{% var random_house = houses | rand %}
Get a random item from the list. Prevents the page from being fast-cached, and every time the page is loaded a new random item will be chosen.

Get one random item from the list and allow fast-caching

{% var random_house = houses | rand:1, false, false %}
Get a random item from the list and allow the page to be fast-cached, resulting in much faster pageload speeds but the same random item will be used until the fast cache expires.

Sort the full list randomly

{% set houses = houses | shuffle %}
Sort the full list randomly. The page will not be able to be fast-cached, and every time the page is loaded a new random order will be chosen. To allow the page to be fast-cached, pass false to the shuffle command.

Various ways to filter and slice

{%- var one_rooms = houses | where: "rooms", "1" | sort: "price" -%}
{%- var cheapest = one_rooms | first -%}
{%- var mid_houses = one_rooms | slice: 1, 6 -%}
{%- var expensive_houses = one_rooms | slice: 7 -%}
{%- var num_expensive_houses = expensive_houses | size -%}

Advanced filter

{% var few_rooms = houses | where_exp: "house", "house.rooms.value < 3" %}

Grouping

{%- var grouped_by_rooms = houses | group_by: "rooms" | sort: "Key" -%}
{%- for list in grouped_by_rooms -%}
<p>{{list.Key | default: "Unknown"}} Rooms ({{list.Value | size }} houses)</p>
{%- endfor -%}

Mapping and Compact

{%- var mapped_by_rooms = grouped_by_rooms | map: "Value" -%}
{%- for list in mapped_by_rooms -%}
<h4>{{list[0].rooms | default: "Unknown"}} Rooms</h4>
<ul>
<li>{{list | map: "description" | compact | join: "</li><li>" }}</li>
</ul>
{%- endfor -%}

Related

{{ group_field }}

Should be used to display a group of fields inside a form.

index filter

Returns the 0-based index of the first occurrence of find in the current list or string, or -1 if it cannot be found. If start is greater than 0, the search will begin at the specified index.

index: object findInteger startBoolean ignorecase

If the input is a string, will search for the index of find as a string in the input. If the input is a list, will search for the index of find in the list. If the input is neither a list or an object, the index filter will return 0 if find is the same as the input or -1 if it is not the same.

Examples

Use the index and last_index filters to find a specific item in a list.

Find something in a list

Copy
{% var list = "abcabcd" | split %}

index

{{ list | index: "a" }}
0
{{ list | index: "c" }}
2
{{ list | index: "A" }}
-1
{{ list | index: "a", 1 }}
3
{{ list | index: "A", 1, true }}
0

last_index

{{ list | last_index: "a" }}
3
{{ list | last_index: "a", 2 }}
0
{{ list | last_index: "A" }}
-1
{{ list | last_index: "A", -1, true }}
3

Related

index filter

Returns the 0-based location of the find string in the current string, or -1 if it cannot be found. If start is greater than 0, the search will begin at the specified index. To ignore capitalization, set ignorecase to true.

index: String findInteger startBoolean ignorecase

Examples

Use the index and last_index filters on a string to find the position of a substring. index searches forward; last_index searches backward.

Finding substring position with index and last_index

Copy
{% var input = 'ABC About' %}

index: first occurrence

{{input | index: 'A'}}
0

index: start after position

{{input | index: 'A', 1}}
4

index: substring

{{input | index: 'Ab'}}
4

index: with case-insensitive flag

{{input | index: 'Ab', 0, true}}
0

index: not found

{{input | index: 'D'}}
-1

last_index: last occurrence

{{input | last_index: 'A'}}
4

last_index: search backward from position

{{input | last_index: 'A', 3}}
0

last_index: substring

{{input | last_index: 'AB'}}
0

last_index: with case-insensitive flag

{{input | last_index: 'AB', -1, true}}
4

last_index: not found

{{input | last_index: 'a'}}
-1

Related

join filter

Returns a string with each element from the input list joined together with the glue string between elements.

join

If the input is not a list, it will be treated as a list with a single object. If either is null, it will be treated as an empty list. All objects in the list will be converted to a string using their default string output if they are not already strings.

Examples

Use the join filter to join a list of strings together. Items in the list that are not already strings will be converted to a string before being joined together.

Join a list into a single string

Copy
{{ "abcdefg" | split | join }}
a b c d e f g
{{ "abcdefg" | split | join:',' }}
a,b,c,d,e,f,g
{%- var list = null -%}
{%- for i in (1..5) -%}
{%- set list = list | concat: i -%}
{%- endfor -%}
{{- list | join: ' and ' }}
1 and 2 and 3 and 4 and 5
{%- templates templates = path:'/examples/dynamically_included' sort_by: 'name' sort_direction:'desc' -%}
<div class="primary">{{ templates | join: '</div><div class="secondary">' }}</div>
<div class="primary">... output from first template</div><div class="secondary">... output from second template</div><div class="secondary">... output from third template, etc...</div>

Related

map filter

Return a new list with the given property from every object in the input list.

map: String property

If the input is null, it will be treated as an empty list. If it is not null and not a list, it will be treated as a list with a single object.

Examples

Use the map filter to map blog posts to their linked title. The map filter could just as easily be used for any other property as well.

Map a list of blog posts to their linked titles

Copy
{%- blog_posts posts = limit: 10 sort_by: 'post_date' sort_direction:'desc' -%}
{{- posts | map: 'linked_title' | join: '<br />' }}

Use the list methods and filters to create and manage lists of data in your templates

Getting and manipulating entity lists

Copy
{%- datastore_items var featured_houses = datastore:"houses" query:"is_featured = true" -%}
{%- datastore_items var houses_by_folder = datastore:"houses" folder:entity.folder -%}

Concatenate and uniq the easy way

{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

{% var houses = featured_houses | concat: houses_by_folder %}

Unique manually

{% var houses = houses | uniq %}

Get one random item from the list

{% var random_house = houses | rand %}
Get a random item from the list. Prevents the page from being fast-cached, and every time the page is loaded a new random item will be chosen.

Get one random item from the list and allow fast-caching

{% var random_house = houses | rand:1, false, false %}
Get a random item from the list and allow the page to be fast-cached, resulting in much faster pageload speeds but the same random item will be used until the fast cache expires.

Sort the full list randomly

{% set houses = houses | shuffle %}
Sort the full list randomly. The page will not be able to be fast-cached, and every time the page is loaded a new random order will be chosen. To allow the page to be fast-cached, pass false to the shuffle command.

Various ways to filter and slice

{%- var one_rooms = houses | where: "rooms", "1" | sort: "price" -%}
{%- var cheapest = one_rooms | first -%}
{%- var mid_houses = one_rooms | slice: 1, 6 -%}
{%- var expensive_houses = one_rooms | slice: 7 -%}
{%- var num_expensive_houses = expensive_houses | size -%}

Advanced filter

{% var few_rooms = houses | where_exp: "house", "house.rooms.value < 3" %}

Grouping

{%- var grouped_by_rooms = houses | group_by: "rooms" | sort: "Key" -%}
{%- for list in grouped_by_rooms -%}
<p>{{list.Key | default: "Unknown"}} Rooms ({{list.Value | size }} houses)</p>
{%- endfor -%}

Mapping and Compact

{%- var mapped_by_rooms = grouped_by_rooms | map: "Value" -%}
{%- for list in mapped_by_rooms -%}
<h4>{{list[0].rooms | default: "Unknown"}} Rooms</h4>
<ul>
<li>{{list | map: "description" | compact | join: "</li><li>" }}</li>
</ul>
{%- endfor -%}

Related

{% map %}

Creates a new list of strings from the output when a block of liquid markup is executed for each item in a list or collection.

rand filter

Returns a random value. Can behave differently depending on both the type of input and on the arguments supplied. Unless prevent_cache is false, the rand filter will prevent the page from being fast-cached. Note: the rand filter should NOT be considered cryptographically secure - do not use in places where cryptographic security is a requirement (ie: do not use to generate random passwords).

rand: Integer lengthBoolean allow_repeatsBoolean prevent_cache

This filter behaves differently depending on the type of input supplied:
Number - If the input is an integer and length is 1, returns a new integer between 1 and the input value. If the input is an integer and length is greater than 1, returns a new list of length numbers between 1 and the input value. If allow_repeats is false, the list returned will be unique. This may result in a list with fewer than length items if the input is less than length.
String - If the input is a string, returns a new random string with length characters, where each character comes directly from the input. If allow_repeats is false, no characters from the input will be used more than once (although any character repeated in the input may be repeated up to the same number of times in the resulting string) - which may result in a string shorter than length if the input string is shorter than length.
List - If the input is a list or list-like object and length is 1, returns a random object from the list. If the input is a list or list-like object and length is greater than 1, returns a new random list of length items from the input list. If allow_repeats is false, no items from the input will be used more than once (although any repeated items in the input may be repeated up to the same number of times in the resulting list) - which may result in a list with fewer than length items if the input list has fewer than length items.

Examples

Use the rand filter to pick one or more random items from a list, get random numbers, or build random strings. Assign results to a variable so the same random choice is used everywhere on the page; use prevent_cache: false when you want the page response to be cacheable.

Getting random items with the rand filter

Copy

One random item from an existing list (prevents fast-caching)

{%- blog_posts posts = start:1 limit:10 -%}
{%- var randompost = posts | rand -%}
<p>Random pick: {{ randompost.linked_title }}</p>
Get a random item from an existing list. Every time the page is loaded a new random item will be chosen.

One random item from an existing list (allows fast-caching)

{%- blog_posts posts = start:1 limit:10 -%}
{%- var randompost = posts | rand:1, false, false -%}
<p>Fast-Cached Random pick: {{ randompost.linked_title }}</p>
Get a random item from an existing list and allow the page to be fast-cached. Subsequent pageloads will have the same random item chosen until the cache expires unless there is something else on the page that prevents fast-caching.

Improved: Fetch only one item from the database

{%- blog_posts intermediate = limit:1 sort_by:'random' cache_random:true -%}
{%- var randompost = intermediate | first -%}
<p>Today's pick: {{ randompost.linked_title }}</p>
Functionally equivalent to the previous example, but only one item has to be fetched from the database. With the cache_random:true argument the result may be fast-cached and can be reused for subsequent pageloads and without the cache_random argument the page will not able to be fast-cached.

Multiple random items, no duplicates

{%- var numbers = (10..20) -%}
{%- var randomitems = numbers | rand:3, false -%}
{{- randomitems | join:' ' -}}
Get 3 random numbers between 10 and 20 without repeats and outputs them as a space-separated list. Because the third argument is not provided the page will not be fast-cached and every pageload will have a new set of random numbers.

Multiple random numbers with duplicates allowed

{%- var rolls = 6 | rand: 4 -%}
<p>Dice rolls: {{ rolls | join: ', ' }}</p>
Get 4 random numbers between 1 and 6 and output them as a comma-separated list. Because the second argument is not provided, duplicates are allowed. Because the third argument is not provided, the page will not be fast-cached and every pageload will have a new set of random numbers.

Multiple random numbers with duplicates allowed and fast-cacheable

{%- var rolls = 6 | rand: 4, true, false -%}
<p>Dice rolls: {{ rolls | join: ', ' }}</p>
Functionally identical to the previous example except the page may be fast-cached so that future pageloads are significantly faster, but may include the same set of random numbers until the cache expires.

Multiple unique random numbers

{%- var picks = 10 | rand: 4, false -%}
<p>Unique picks: {{ picks | join: ' ' }}</p>
Gets 4 unique random numbers between 1 and 10 and outputs them as a space-separated list. Because the third argument is not provided, the page will not be fast-cached and every pageload will have a new set of random numbers.

Single random number between 0 and 10

{{ 11 | rand: 1 | minus: 1 }}
To get a random number starting with 0 we need to subtract 1 from the result. To allow the page to be fast-cached: Specify either true or false for the second argument (it doesn't matter which for this example) and specify false for the third argument.

Random character from a string (cache in page)

{{ "aaabcdeeefghjkmnpqrstuuuvwxyz123456789-_" | rand }}
Gets a single random character from the set of characters in the string. In this example, the set of characters includes lowercase letters, numbers, and the hyphen and underscore characters with some characters excluded and some repeated to increase their probability of being chosen.

Random alphanumeric string

{{ "abcdefghijklmnopqrstuvwxyz0123456789" | rand: 10 }}
Pick 10 random characters from the set, with duplicates allowed. Because the third argument is not provided, the page will not be fast-cached and every pageload will have a new set of random characters.

Random alphanumeric string without duplciates

{{ "abcdefghijklmnopqrstuvwxyz0123456789-----_____" | rand: 10, false }}
Pick 10 random characters from the set, with no duplicates allowed. However, because the set itself contains 5 hyphens and 5 underscores the result may also contain up to 5 hyphens and 5 underscores. Because the third argument is provided as false, the page will not be fast-cached and every pageload will have a new set of random characters.

Related

size filter

Returns the length of the input string or list.

size

If the input is not a string or list returns 0.

Examples

Use the size filter to get the size of a string or a list

Using the size filter

Copy

When used on null input

{{null | size}}
0

When used on a string

{{"string" | size}}
6

When used on a list

{{"item1,item2,item3" | split:"," | size}}
3

When used on anything else

{{request.date | size}}
0

Related

uniq filter

Remove all duplicate objects in the input list. If property is specified, objects are considered duplicate if their property value is the same.

uniq: String propertyBoolean ignorecase

If the input is null it will return an empty list. If the input is not null and is not a list, it will be treated as a list with a single object.

Examples

Use the uniq filter to remove duplicate items from a list.

Removing duplicates from a list

Copy
{{'bookkeeper' | split: '' | uniq}}
bokepr
{{'A B c d a b c' | split: ' ' | uniq}}
ABcdab
{{'A B c d a b c' | split: ' ' | uniq: null, true}}
ABcd

Unique by property

{%- blog_posts posts = limit:20 sort_by:'date_posted' sort_direction:'desc' -%}
{%- var latest_by_blog = posts | uniq:'blog_guid' -%}

Unique by property

{% var one_per_category = items | unique:'category', true %}

Use the list methods and filters to create and manage lists of data in your templates

Getting and manipulating entity lists

Copy
{%- datastore_items var featured_houses = datastore:"houses" query:"is_featured = true" -%}
{%- datastore_items var houses_by_folder = datastore:"houses" folder:entity.folder -%}

Concatenate and uniq the easy way

{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

{% var houses = featured_houses | concat: houses_by_folder %}

Unique manually

{% var houses = houses | uniq %}

Get one random item from the list

{% var random_house = houses | rand %}
Get a random item from the list. Prevents the page from being fast-cached, and every time the page is loaded a new random item will be chosen.

Get one random item from the list and allow fast-caching

{% var random_house = houses | rand:1, false, false %}
Get a random item from the list and allow the page to be fast-cached, resulting in much faster pageload speeds but the same random item will be used until the fast cache expires.

Sort the full list randomly

{% set houses = houses | shuffle %}
Sort the full list randomly. The page will not be able to be fast-cached, and every time the page is loaded a new random order will be chosen. To allow the page to be fast-cached, pass false to the shuffle command.

Various ways to filter and slice

{%- var one_rooms = houses | where: "rooms", "1" | sort: "price" -%}
{%- var cheapest = one_rooms | first -%}
{%- var mid_houses = one_rooms | slice: 1, 6 -%}
{%- var expensive_houses = one_rooms | slice: 7 -%}
{%- var num_expensive_houses = expensive_houses | size -%}

Advanced filter

{% var few_rooms = houses | where_exp: "house", "house.rooms.value < 3" %}

Grouping

{%- var grouped_by_rooms = houses | group_by: "rooms" | sort: "Key" -%}
{%- for list in grouped_by_rooms -%}
<p>{{list.Key | default: "Unknown"}} Rooms ({{list.Value | size }} houses)</p>
{%- endfor -%}

Mapping and Compact

{%- var mapped_by_rooms = grouped_by_rooms | map: "Value" -%}
{%- for list in mapped_by_rooms -%}
<h4>{{list[0].rooms | default: "Unknown"}} Rooms</h4>
<ul>
<li>{{list | map: "description" | compact | join: "</li><li>" }}</li>
</ul>
{%- endfor -%}

Related

last filter

Returns the last item in the input list

last

If the input is not a list, it will be returned without modification.

Examples

Use the first and last filter to get a specific item in a list. In some cases, you can use square bracket notation to get an item at a specific index.

Getting a specific item from a list

Copy
{% var input = 'a,b,c,d' | split:',' %}

first

{{input | first}}
a

last

{{input | last}}
d

square bracket

{{input[2]}}
c
[{{input[5]}}]
[]
{{input[-1]}}
d

Related

last_index filter

Returns the last 0-based location of the last occurrence of find string in the current string, or -1 if it cannot be found. If start is greater than or equal to 0, the search will begin at the specified index. To ignore capitalization, set ignorecase to true.

last_index: String findInteger startBoolean ignorecase

Related

last_index filter

Returns the 0-based index of the last occurrence of find object in the current list or string, or -1 if it cannot be found. If start is greater than -1, the search will begin at the specified index.

last_index: object findInteger startBoolean ignorecase

If the input is a string, will search for the last index of find as a string in the input. If the input is a list, will search for the last index of find in the list. If the input is neither a list or an object, the index filter will return 0 if find is the same as the input or -1 if it is not the same.

Examples

Use the index and last_index filters to find a specific item in a list.

Find something in a list

Copy
{% var list = "abcabcd" | split %}

index

{{ list | index: "a" }}
0
{{ list | index: "c" }}
2
{{ list | index: "A" }}
-1
{{ list | index: "a", 1 }}
3
{{ list | index: "A", 1, true }}
0

last_index

{{ list | last_index: "a" }}
3
{{ list | last_index: "a", 2 }}
0
{{ list | last_index: "A" }}
-1
{{ list | last_index: "A", -1, true }}
3

Related

reverse filter

Reverses the input string or list.

reverse

When used with a string as input, the result is a string. Otherwise the result is a list.

Examples

Use the reverse filter to reverse either a string or a list

Using the reverse filter

Copy

When used on null input

{{null | reverse | object_type}}
null

When used on a string

{{"string" | reverse}}
gnirts

When used on a list

{{"item1,item2,item3" | split:"," | reverse | join:" "}}
item3 item2 item1

Related

shuffle filter

Sorts the input list randomly. Unless prevent_cache is false, the shuffle filter will prevent the page from being fast-cached.

shuffle: Boolean prevent_cache

If the input is null it will return an empty list. If the input is not null and is not a list, it will be treated as a list with a single object.

Examples

Use the sort, sort_natural, and shuffle filters to sort lists of objects. For consistency and readability, the sort filter should be preferred to the sort_natural filter.

Sorting lists

Copy
{%- var stringinputs = 'JucLPXeHBgaZokTRsymNqwFViD' | split: '' -%}
{%- var numberinputs = (1..10) | shuffle -%}
{{- numberinputs | json_encode }}
[7,6,5,9,2,3,8,1,10,4]

Sort strings

{{ stringinputs | sort | join: '' }}
BDFHJLNPRTVXZacegikmoqsuwy
The sort filter sorts the strings in ascending order, with capital letters coming before lowercase letters.

Sort and ignore case

{{ stringinputs | sort: null, true | join: '' }}
aBcDeFgHiJkLmNoPqRsTuVwXyZ
By passing true as the second argument, the sort filter will ignore capitalization when comparing strings.

Sort and ignore case using sort_natural

{{ stringinputs | sort_natural | join: '' }}
aBcDeFgHiJkLmNoPqRsTuVwXyZ
The sort_natural filter is the same as the sort filter with the second argument set to true.

Sort numbers

{{ numberinputs | sort | join: ' ' }}
1 2 3 4 5 6 7 8 9 10
The sort filter sorts the numbers in ascending order.

Sort objects

{% var sorted = houses | sort: 'number_of_rooms' %}
To sort objects, pass in the property to sort by. In this example, the list of houses will be sorted by the "number_of_rooms" property.

Sort objects by string property and ignore case

{% var sorted = houses | sort: 'name', true %}
When sorting objects by a string property, remember to set the second argument to true or else uppercase letters will be sorted higher before lowercase letters.

Sort randomly

{{ stringinputs | shuffle | join: '' }}
JDgRoPNTFckXsmBHZqawVeyuLi
The shuffle filter sorts the strings in a random order. The page will not be able to be fast-cached, and every time the page is loaded a new random order will be chosen.

Sort randomly and allow fast-caching

{{ stringinputs | shuffle: false | join: '' }}
kjFQDXZTVgEruhPbHnCwIYMzASO
Pass false to the shuffle command to specify that the page may still be fast-cached, resulting in much faster pageload times but the same random result will be chosen on every pageload until the fast cache expires.

Sort randomly using the sort filter with the special string "random" as the property

{{ numberinputs | sort: 'random' | join }}
9 5 4 10 6 2 1 3 7 8
Passing "random" as the first argument to the sort filter is functionally identical to using the shuffle filter. In most cases, the shuffle filter should be preferred for clarity.

Use the list methods and filters to create and manage lists of data in your templates

Getting and manipulating entity lists

Copy
{%- datastore_items var featured_houses = datastore:"houses" query:"is_featured = true" -%}
{%- datastore_items var houses_by_folder = datastore:"houses" folder:entity.folder -%}

Concatenate and uniq the easy way

{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

{% var houses = featured_houses | concat: houses_by_folder %}

Unique manually

{% var houses = houses | uniq %}

Get one random item from the list

{% var random_house = houses | rand %}
Get a random item from the list. Prevents the page from being fast-cached, and every time the page is loaded a new random item will be chosen.

Get one random item from the list and allow fast-caching

{% var random_house = houses | rand:1, false, false %}
Get a random item from the list and allow the page to be fast-cached, resulting in much faster pageload speeds but the same random item will be used until the fast cache expires.

Sort the full list randomly

{% set houses = houses | shuffle %}
Sort the full list randomly. The page will not be able to be fast-cached, and every time the page is loaded a new random order will be chosen. To allow the page to be fast-cached, pass false to the shuffle command.

Various ways to filter and slice

{%- var one_rooms = houses | where: "rooms", "1" | sort: "price" -%}
{%- var cheapest = one_rooms | first -%}
{%- var mid_houses = one_rooms | slice: 1, 6 -%}
{%- var expensive_houses = one_rooms | slice: 7 -%}
{%- var num_expensive_houses = expensive_houses | size -%}

Advanced filter

{% var few_rooms = houses | where_exp: "house", "house.rooms.value < 3" %}

Grouping

{%- var grouped_by_rooms = houses | group_by: "rooms" | sort: "Key" -%}
{%- for list in grouped_by_rooms -%}
<p>{{list.Key | default: "Unknown"}} Rooms ({{list.Value | size }} houses)</p>
{%- endfor -%}

Mapping and Compact

{%- var mapped_by_rooms = grouped_by_rooms | map: "Value" -%}
{%- for list in mapped_by_rooms -%}
<h4>{{list[0].rooms | default: "Unknown"}} Rooms</h4>
<ul>
<li>{{list | map: "description" | compact | join: "</li><li>" }}</li>
</ul>
{%- endfor -%}

Related

slice filter

Return a part of the current string or list.

slice: Integer startInteger length

If the input is a string this will return a string. If it is a list it will return a list. Otherwise it will not do anything and will return the unaltered input.

Examples

Use the slice filter to get a portion of a string or list.

Using the slice filter

Copy

When used on null input

{{null | slice | object_type}}
null

When used on a string

{{"string" | slice: 2}}
ring
{{"string" | slice: 2, 3}}
rin

When used on a list

{{"ab,cd,ef,gh,ij,kl,mn,op,qr,st,uv,wx,yz" | split:"," | slice: 2, 3 | join: " "}}
ef gh ij

With a negative start value

{{"string" | slice: -2}}
ng

With a negative length

{{"string" | slice: 2, -1}}
rin

With a negative start and length

{{"string" | slice: -4, -2}}
ri

With length = 0

{{"string" | slice: -4, 0}}
ring

With a really large negative start

{{"string" | slice: -20}}
string

With start higher than the input length

[{{"string" | slice: 20}}]
[]

With a calculated length less or equal to 0

[{{"string" | slice: 2, -4}}]
[]

Does not do anything if the object is not a string or a list

{{ request.date | slice: 2 }}
9/9/2009 12:00:00 AM

Related

sort filter

Sort objects in the input list. If the property is specified, use it to sort objects in the list. Use the special string value &quot;random&quot; to sort the list in a random order - which is functionally identical to using the shuffle filter.

sort: String propertyBoolean ignorecase

If the input is null it will return an empty list. If the input is not null and is not a list, it will be treated as a list with a single object.

Examples

Use the sort, sort_natural, and shuffle filters to sort lists of objects. For consistency and readability, the sort filter should be preferred to the sort_natural filter.

Sorting lists

Copy
{%- var stringinputs = 'JucLPXeHBgaZokTRsymNqwFViD' | split: '' -%}
{%- var numberinputs = (1..10) | shuffle -%}
{{- numberinputs | json_encode }}
[7,6,5,9,2,3,8,1,10,4]

Sort strings

{{ stringinputs | sort | join: '' }}
BDFHJLNPRTVXZacegikmoqsuwy
The sort filter sorts the strings in ascending order, with capital letters coming before lowercase letters.

Sort and ignore case

{{ stringinputs | sort: null, true | join: '' }}
aBcDeFgHiJkLmNoPqRsTuVwXyZ
By passing true as the second argument, the sort filter will ignore capitalization when comparing strings.

Sort and ignore case using sort_natural

{{ stringinputs | sort_natural | join: '' }}
aBcDeFgHiJkLmNoPqRsTuVwXyZ
The sort_natural filter is the same as the sort filter with the second argument set to true.

Sort numbers

{{ numberinputs | sort | join: ' ' }}
1 2 3 4 5 6 7 8 9 10
The sort filter sorts the numbers in ascending order.

Sort objects

{% var sorted = houses | sort: 'number_of_rooms' %}
To sort objects, pass in the property to sort by. In this example, the list of houses will be sorted by the "number_of_rooms" property.

Sort objects by string property and ignore case

{% var sorted = houses | sort: 'name', true %}
When sorting objects by a string property, remember to set the second argument to true or else uppercase letters will be sorted higher before lowercase letters.

Sort randomly

{{ stringinputs | shuffle | join: '' }}
JDgRoPNTFckXsmBHZqawVeyuLi
The shuffle filter sorts the strings in a random order. The page will not be able to be fast-cached, and every time the page is loaded a new random order will be chosen.

Sort randomly and allow fast-caching

{{ stringinputs | shuffle: false | join: '' }}
kjFQDXZTVgEruhPbHnCwIYMzASO
Pass false to the shuffle command to specify that the page may still be fast-cached, resulting in much faster pageload times but the same random result will be chosen on every pageload until the fast cache expires.

Sort randomly using the sort filter with the special string "random" as the property

{{ numberinputs | sort: 'random' | join }}
9 5 4 10 6 2 1 3 7 8
Passing "random" as the first argument to the sort filter is functionally identical to using the shuffle filter. In most cases, the shuffle filter should be preferred for clarity.

Use the list methods and filters to create and manage lists of data in your templates

Getting and manipulating entity lists

Copy
{%- datastore_items var featured_houses = datastore:"houses" query:"is_featured = true" -%}
{%- datastore_items var houses_by_folder = datastore:"houses" folder:entity.folder -%}

Concatenate and uniq the easy way

{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

{% var houses = featured_houses | concat: houses_by_folder %}

Unique manually

{% var houses = houses | uniq %}

Get one random item from the list

{% var random_house = houses | rand %}
Get a random item from the list. Prevents the page from being fast-cached, and every time the page is loaded a new random item will be chosen.

Get one random item from the list and allow fast-caching

{% var random_house = houses | rand:1, false, false %}
Get a random item from the list and allow the page to be fast-cached, resulting in much faster pageload speeds but the same random item will be used until the fast cache expires.

Sort the full list randomly

{% set houses = houses | shuffle %}
Sort the full list randomly. The page will not be able to be fast-cached, and every time the page is loaded a new random order will be chosen. To allow the page to be fast-cached, pass false to the shuffle command.

Various ways to filter and slice

{%- var one_rooms = houses | where: "rooms", "1" | sort: "price" -%}
{%- var cheapest = one_rooms | first -%}
{%- var mid_houses = one_rooms | slice: 1, 6 -%}
{%- var expensive_houses = one_rooms | slice: 7 -%}
{%- var num_expensive_houses = expensive_houses | size -%}

Advanced filter

{% var few_rooms = houses | where_exp: "house", "house.rooms.value < 3" %}

Grouping

{%- var grouped_by_rooms = houses | group_by: "rooms" | sort: "Key" -%}
{%- for list in grouped_by_rooms -%}
<p>{{list.Key | default: "Unknown"}} Rooms ({{list.Value | size }} houses)</p>
{%- endfor -%}

Mapping and Compact

{%- var mapped_by_rooms = grouped_by_rooms | map: "Value" -%}
{%- for list in mapped_by_rooms -%}
<h4>{{list[0].rooms | default: "Unknown"}} Rooms</h4>
<ul>
<li>{{list | map: "description" | compact | join: "</li><li>" }}</li>
</ul>
{%- endfor -%}

Related

sort_natural filter

Sort objects in the input list. If the property is specified, use it to sort objects in the list. Use the special string value &quot;random&quot; to sort the list in a random order - which is functionally identical to using the shuffle filter. The sort_natural filter ignores capitalization while the sort filter does not by default. For readability and consistency, it is advised to use the sort filter with the ignorecase property set to true instead of the sort_natural filter.

sort_natural: String property

If the input is null it will return an empty list. If the input is not null and is not a list, it will be treated as a list with a single object.

Examples

Use the sort, sort_natural, and shuffle filters to sort lists of objects. For consistency and readability, the sort filter should be preferred to the sort_natural filter.

Sorting lists

Copy
{%- var stringinputs = 'JucLPXeHBgaZokTRsymNqwFViD' | split: '' -%}
{%- var numberinputs = (1..10) | shuffle -%}
{{- numberinputs | json_encode }}
[7,6,5,9,2,3,8,1,10,4]

Sort strings

{{ stringinputs | sort | join: '' }}
BDFHJLNPRTVXZacegikmoqsuwy
The sort filter sorts the strings in ascending order, with capital letters coming before lowercase letters.

Sort and ignore case

{{ stringinputs | sort: null, true | join: '' }}
aBcDeFgHiJkLmNoPqRsTuVwXyZ
By passing true as the second argument, the sort filter will ignore capitalization when comparing strings.

Sort and ignore case using sort_natural

{{ stringinputs | sort_natural | join: '' }}
aBcDeFgHiJkLmNoPqRsTuVwXyZ
The sort_natural filter is the same as the sort filter with the second argument set to true.

Sort numbers

{{ numberinputs | sort | join: ' ' }}
1 2 3 4 5 6 7 8 9 10
The sort filter sorts the numbers in ascending order.

Sort objects

{% var sorted = houses | sort: 'number_of_rooms' %}
To sort objects, pass in the property to sort by. In this example, the list of houses will be sorted by the "number_of_rooms" property.

Sort objects by string property and ignore case

{% var sorted = houses | sort: 'name', true %}
When sorting objects by a string property, remember to set the second argument to true or else uppercase letters will be sorted higher before lowercase letters.

Sort randomly

{{ stringinputs | shuffle | join: '' }}
JDgRoPNTFckXsmBHZqawVeyuLi
The shuffle filter sorts the strings in a random order. The page will not be able to be fast-cached, and every time the page is loaded a new random order will be chosen.

Sort randomly and allow fast-caching

{{ stringinputs | shuffle: false | join: '' }}
kjFQDXZTVgEruhPbHnCwIYMzASO
Pass false to the shuffle command to specify that the page may still be fast-cached, resulting in much faster pageload times but the same random result will be chosen on every pageload until the fast cache expires.

Sort randomly using the sort filter with the special string "random" as the property

{{ numberinputs | sort: 'random' | join }}
9 5 4 10 6 2 1 3 7 8
Passing "random" as the first argument to the sort filter is functionally identical to using the shuffle filter. In most cases, the shuffle filter should be preferred for clarity.

Related

times filter

Multiply the input by the operand.

times: Number operand

This filter currently behaves differently if the input is a string and the operand is an integer. In that case the result is a list of strings with input repeated operand times.

Examples

Use the times filter to multiply a number by another number

Multiply numbers

Copy
{{4 | times:2}}
8
{{4.1 | times:-2.2}}
-9.02

You can currently use the times to multiply a string into a list of identical strings, although this behavior is deprecated. If you need this functionality you are advised to find a different way to accomplish it.

Using the times filter with a string

Copy

Current functionality

{{"string" | times:2 | json_encode}}
["string","string"]

Future functionality

{{"string" | times:2 | json_encode}}
Liquid error

Potential Replacement

{%- map string for i in (1..2) %}string{% endmap -%}
{{-string | json_encode}}
["string","string"]

Alternate Replacement

{%- capture string %}{% for i in (1..2) %}string{% unless forloop.last %},{% endunless %}{% endfor %}{% endcapture -%}
{{-string | split:',' | json_encode}}
["string","string"]

Related

{{ time }}

Represents a specific instant in a specific timezone.

split filter

Split a string into a list of substrings separated by the given separator

split: String separator

Examples

Use the split filter to convert a string into a list of strings separated by the given separator

Using the split filter

Copy
{{'The quick brown fox jumps over the lazy dog.' | split:" " | json_encode}}
["The","quick","brown","fox","jumps","over","the","lazy","dog."]

Split on multiple characters

{{'She sells sea shells on the sea shore.' | split:"sea" | json_encode}}
["She sells "," shells on the "," shore."]

Does not include empty strings in the result

{{"110100101" | split:'1' | json_encode}}
["0","00","0" | split:'1'}}

Use an empty pattern to split the string into individual characters

{{"abcdef" | split:'' | json_encode}}
["a","b","c","d","e","f"]

Related

where filter

Returns a new list which only contains items from the input list where the property has the specified value.

where: String propertyobject valueBoolean ignorecase

If the input is null it will return an empty list. If the input is not null and is not a list, it will be treated as a list with a single object. If the property to filter by is null, the input list will be unfiltered.

Examples

Use the where and where_exp filters to get a new list containing only the items from the input list that match the provided condition.

Filtering lists

Copy
{% var arr = existing_list | where: 'priority', 1 %}
{% var arr = existing_list | where: 'position', 'top', true %}
{% var with_rooms = houses | where_exp: 'house', 'house.rooms.value >= 2' %}

Use the list methods and filters to create and manage lists of data in your templates

Getting and manipulating entity lists

Copy
{%- datastore_items var featured_houses = datastore:"houses" query:"is_featured = true" -%}
{%- datastore_items var houses_by_folder = datastore:"houses" folder:entity.folder -%}

Concatenate and uniq the easy way

{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

{% var houses = featured_houses | concat: houses_by_folder %}

Unique manually

{% var houses = houses | uniq %}

Get one random item from the list

{% var random_house = houses | rand %}
Get a random item from the list. Prevents the page from being fast-cached, and every time the page is loaded a new random item will be chosen.

Get one random item from the list and allow fast-caching

{% var random_house = houses | rand:1, false, false %}
Get a random item from the list and allow the page to be fast-cached, resulting in much faster pageload speeds but the same random item will be used until the fast cache expires.

Sort the full list randomly

{% set houses = houses | shuffle %}
Sort the full list randomly. The page will not be able to be fast-cached, and every time the page is loaded a new random order will be chosen. To allow the page to be fast-cached, pass false to the shuffle command.

Various ways to filter and slice

{%- var one_rooms = houses | where: "rooms", "1" | sort: "price" -%}
{%- var cheapest = one_rooms | first -%}
{%- var mid_houses = one_rooms | slice: 1, 6 -%}
{%- var expensive_houses = one_rooms | slice: 7 -%}
{%- var num_expensive_houses = expensive_houses | size -%}

Advanced filter

{% var few_rooms = houses | where_exp: "house", "house.rooms.value < 3" %}

Grouping

{%- var grouped_by_rooms = houses | group_by: "rooms" | sort: "Key" -%}
{%- for list in grouped_by_rooms -%}
<p>{{list.Key | default: "Unknown"}} Rooms ({{list.Value | size }} houses)</p>
{%- endfor -%}

Mapping and Compact

{%- var mapped_by_rooms = grouped_by_rooms | map: "Value" -%}
{%- for list in mapped_by_rooms -%}
<h4>{{list[0].rooms | default: "Unknown"}} Rooms</h4>
<ul>
<li>{{list | map: "description" | compact | join: "</li><li>" }}</li>
</ul>
{%- endfor -%}

Related

where_exp filter

Returns a new list which only contains items from the input list that match the given expression when the item is referenced as name.

where_exp: String nameString expression

If the input is null it will return an empty list. If the input is not null and is not a list, it will be treated as a list with a single object. If either name or expression are null or empty, the input list will be unfiltered.

Examples

Use the where and where_exp filters to get a new list containing only the items from the input list that match the provided condition.

Filtering lists

Copy
{% var arr = existing_list | where: 'priority', 1 %}
{% var arr = existing_list | where: 'position', 'top', true %}
{% var with_rooms = houses | where_exp: 'house', 'house.rooms.value >= 2' %}

Use the list methods and filters to create and manage lists of data in your templates

Getting and manipulating entity lists

Copy
{%- datastore_items var featured_houses = datastore:"houses" query:"is_featured = true" -%}
{%- datastore_items var houses_by_folder = datastore:"houses" folder:entity.folder -%}

Concatenate and uniq the easy way

{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

{% var houses = featured_houses | concat: houses_by_folder %}

Unique manually

{% var houses = houses | uniq %}

Get one random item from the list

{% var random_house = houses | rand %}
Get a random item from the list. Prevents the page from being fast-cached, and every time the page is loaded a new random item will be chosen.

Get one random item from the list and allow fast-caching

{% var random_house = houses | rand:1, false, false %}
Get a random item from the list and allow the page to be fast-cached, resulting in much faster pageload speeds but the same random item will be used until the fast cache expires.

Sort the full list randomly

{% set houses = houses | shuffle %}
Sort the full list randomly. The page will not be able to be fast-cached, and every time the page is loaded a new random order will be chosen. To allow the page to be fast-cached, pass false to the shuffle command.

Various ways to filter and slice

{%- var one_rooms = houses | where: "rooms", "1" | sort: "price" -%}
{%- var cheapest = one_rooms | first -%}
{%- var mid_houses = one_rooms | slice: 1, 6 -%}
{%- var expensive_houses = one_rooms | slice: 7 -%}
{%- var num_expensive_houses = expensive_houses | size -%}

Advanced filter

{% var few_rooms = houses | where_exp: "house", "house.rooms.value < 3" %}

Grouping

{%- var grouped_by_rooms = houses | group_by: "rooms" | sort: "Key" -%}
{%- for list in grouped_by_rooms -%}
<p>{{list.Key | default: "Unknown"}} Rooms ({{list.Value | size }} houses)</p>
{%- endfor -%}

Mapping and Compact

{%- var mapped_by_rooms = grouped_by_rooms | map: "Value" -%}
{%- for list in mapped_by_rooms -%}
<h4>{{list[0].rooms | default: "Unknown"}} Rooms</h4>
<ul>
<li>{{list | map: "description" | compact | join: "</li><li>" }}</li>
</ul>
{%- endfor -%}

Related