Skip to documentation content

List Filters

List Filters

Liquid filters for lists (sort, where, map, etc.).

compact filter

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

compact
Example How to use the compact filterUse the compact filter to remove null, empty, and invalid items from a list (e.g. for cleaner iteration or mapping).

Remove null and empty from a list

Liquid
{%- var mixed = ',,,a,,,b,b,,c,,' | split: ',' -%}
{{- mixed | compact | join: ',' }} <- {{ mixed | join: ',' -}}
Output
a,b,b,c <- ,,,a,,,b,b,,c,,

The list is output twice. The first time it uses compact to remove all of the empty strings from the list and the second time is without modification to show that the original list was not modified. Note that the compact filter does not remove duplicates.

After map or other operations

Liquid
{%- var descriptions = items | map: 'description' | map: 'value' | compact -%}
{%- if descriptions is_valid -%}
	<ul>
		{%- for d in descriptions -%}
			<li>{{ d }}</li>
		{%- endfor -%}
	</ul>
{%- endif -%}

Useful after mapping to a property that may be missing on some items, so you only iterate over present values.

When the list may contain invalid items

Liquid
{%- var valid_props = entity.data.properties | map: 'Value' | compact -%}
{%- if valid_props is_valid -%}
	<dl>
		{%- for p in valid_props -%}
			<dt>{{ p.field_id }}</dt>
			<dd>{{ p.value | escape}}</dd>
		{%- endfor -%}
	</dl>
{%- endif -%}

When the list may contain invalid items, you can use the compact filter to remove them. In this example, entity.data.properties is assumed to contain a list of objects which may contain some invalid objects. The compact filter is then used to remove all of the invalid objects from the list before outputting it to the page.

Example Getting and manipulating entity listsUse the list methods and filters to create and manage lists of data in your templates
Liquid
{%- 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

Liquid
{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

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

Unique manually

Liquid
{% var houses = houses | uniq %}

Get one random item from the list

Liquid
{% 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

Liquid
{% 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

Liquid
{% 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

Liquid
{%- 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

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

Grouping

Liquid
{%- 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

Liquid
{%- 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 -%}

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.

Example Joining lists togetherUse the concat filter to join multiple lists together
Liquid
{% "a" | concat:"b" | json_encode %}
Output
["a", "b"]
Example Getting and manipulating entity listsUse the list methods and filters to create and manage lists of data in your templates
Liquid
{%- 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

Liquid
{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

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

Unique manually

Liquid
{% var houses = houses | uniq %}

Get one random item from the list

Liquid
{% 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

Liquid
{% 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

Liquid
{% 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

Liquid
{%- 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

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

Grouping

Liquid
{%- 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

Liquid
{%- 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 -%}

first filter

Returns the first item in the input list

first

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

Example Getting a specific item from a listUse 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.
Liquid
{% var input = 'a,b,c,d' | split:',' %}

first

Liquid
{{input | first}}
Output
a

last

Liquid
{{input | last}}
Output
d

square bracket

Liquid
{{input[2]}}
Output
c
Liquid
[{{input[5]}}]
Output
[]
Liquid
{{input[-1]}}
Output
d
Example Getting and manipulating entity listsUse the list methods and filters to create and manage lists of data in your templates
Liquid
{%- 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

Liquid
{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

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

Unique manually

Liquid
{% var houses = houses | uniq %}

Get one random item from the list

Liquid
{% 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

Liquid
{% 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

Liquid
{% 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

Liquid
{%- 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

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

Grouping

Liquid
{%- 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

Liquid
{%- 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 -%}

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.

Example Group blog posts by blog (group filter)Use the group_by filter to group objects by a specific property. For example, you can group a list of blog posts by blog.
Liquid
{%- 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 -%}
Output
<p>General Blog: 22</p>
<p>Announcements: 10</p>
<p>Events: 8</p>
Example Getting and manipulating entity listsUse the list methods and filters to create and manage lists of data in your templates
Liquid
{%- 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

Liquid
{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

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

Unique manually

Liquid
{% var houses = houses | uniq %}

Get one random item from the list

Liquid
{% 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

Liquid
{% 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

Liquid
{% 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

Liquid
{%- 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

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

Grouping

Liquid
{%- 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

Liquid
{%- 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 -%}

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 find, integer start, boolean 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.

Example Find something in a listUse the index and last_index filters to find a specific item in a list.
Liquid
{% var list = "abcabcd" | split %}

index

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

last_index

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

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 find, integer start, boolean ignorecase
Example Use the index and last_index filters to find substring positionUse the index and last_index filters on a string to find the position of a substring. index searches forward; last_index searches backward.
Liquid
{% var input = 'ABC About' %}

index: first occurrence

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

index: start after position

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

index: substring

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

index: with case-insensitive flag

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

index: not found

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

last_index: last occurrence

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

last_index: search backward from position

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

last_index: substring

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

last_index: with case-insensitive flag

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

last_index: not found

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

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.

Example Joining lists togetherUse the concat filter to join multiple lists together
Liquid
{% "a" | concat:"b" | json_encode %}
Output
["a", "b"]
Example Join a list into a single stringUse 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.
Liquid
{{ "abcdefg" | split | join }}
Output
a b c d e f g
Liquid
{{ "abcdefg" | split | join:',' }}
Output
a,b,c,d,e,f,g
Liquid
{%- var list = null -%}
{%- for i in (1..5) -%}
{%- set list = list | concat: i -%}
{%- endfor -%}
{{- list | join: ' and ' }}
Output
1 and 2 and 3 and 4 and 5
Liquid
{%- templates templates = path:'/examples/dynamically_included' sort_by: 'name' sort_direction:'desc' -%}
<div class="primary">{{ templates | join: '</div><div class="secondary">' }}</div>
Output
<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>
Example Getting and manipulating entity listsUse the list methods and filters to create and manage lists of data in your templates
Liquid
{%- 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

Liquid
{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

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

Unique manually

Liquid
{% var houses = houses | uniq %}

Get one random item from the list

Liquid
{% 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

Liquid
{% 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

Liquid
{% 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

Liquid
{%- 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

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

Grouping

Liquid
{%- 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

Liquid
{%- 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 -%}

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.

Example Map a list of blog posts to their linked titlesUse 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.
Liquid
{%- blog_posts posts = limit: 10 sort_by: 'post_date' sort_direction:'desc' -%}
{{- posts | map: 'linked_title' | join: '<br />' }}
Example Getting and manipulating entity listsUse the list methods and filters to create and manage lists of data in your templates
Liquid
{%- 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

Liquid
{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

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

Unique manually

Liquid
{% var houses = houses | uniq %}

Get one random item from the list

Liquid
{% 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

Liquid
{% 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

Liquid
{% 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

Liquid
{%- 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

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

Grouping

Liquid
{%- 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

Liquid
{%- 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 -%}

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 length, boolean allow_repeats, boolean 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.

Example Getting random items with the rand filterUse 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.

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

Liquid
{%- 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)

Liquid
{%- 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

Liquid
{%- 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

Liquid
{%- 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

Liquid
{%- 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

Liquid
{%- 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

Liquid
{%- 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

Liquid
{{ 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)

Liquid
{{ "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

Liquid
{{ "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

Liquid
{{ "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.

Example Getting and manipulating entity listsUse the list methods and filters to create and manage lists of data in your templates
Liquid
{%- 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

Liquid
{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

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

Unique manually

Liquid
{% var houses = houses | uniq %}

Get one random item from the list

Liquid
{% 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

Liquid
{% 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

Liquid
{% 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

Liquid
{%- 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

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

Grouping

Liquid
{%- 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

Liquid
{%- 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 -%}

size filter

Returns the length of the input string or list.

size

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

Example Using the size filterUse the size filter to get the size of a string or a list

When used on null input

Liquid
{{null | size}}
Output
0

When used on a string

Liquid
{{"string" | size}}
Output
6

When used on a list

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

When used on anything else

Liquid
{{request.date | size}}
Output
0
Example Getting and manipulating entity listsUse the list methods and filters to create and manage lists of data in your templates
Liquid
{%- 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

Liquid
{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

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

Unique manually

Liquid
{% var houses = houses | uniq %}

Get one random item from the list

Liquid
{% 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

Liquid
{% 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

Liquid
{% 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

Liquid
{%- 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

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

Grouping

Liquid
{%- 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

Liquid
{%- 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 -%}

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 property, boolean 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.

Example Removing duplicates from a listUse the uniq filter to remove duplicate items from a list.
Liquid
{{'bookkeeper' | split: '' | uniq}}
Output
bokepr
Liquid
{{'A B c d a b c' | split: ' ' | uniq}}
Output
ABcdab
Liquid
{{'A B c d a b c' | split: ' ' | uniq: null, true}}
Output
ABcd

Unique by property

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

Unique by property

Liquid
{% var one_per_category = items | unique:'category', true %}
Example Getting and manipulating entity listsUse the list methods and filters to create and manage lists of data in your templates
Liquid
{%- 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

Liquid
{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

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

Unique manually

Liquid
{% var houses = houses | uniq %}

Get one random item from the list

Liquid
{% 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

Liquid
{% 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

Liquid
{% 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

Liquid
{%- 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

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

Grouping

Liquid
{%- 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

Liquid
{%- 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 -%}

last filter

Returns the last item in the input list

last

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

Example Getting a specific item from a listUse 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.
Liquid
{% var input = 'a,b,c,d' | split:',' %}

first

Liquid
{{input | first}}
Output
a

last

Liquid
{{input | last}}
Output
d

square bracket

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

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 find, integer start, boolean ignorecase
Example Use the index and last_index filters to find substring positionUse the index and last_index filters on a string to find the position of a substring. index searches forward; last_index searches backward.
Liquid
{% var input = 'ABC About' %}

index: first occurrence

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

index: start after position

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

index: substring

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

index: with case-insensitive flag

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

index: not found

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

last_index: last occurrence

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

last_index: search backward from position

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

last_index: substring

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

last_index: with case-insensitive flag

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

last_index: not found

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

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 find, integer start, boolean 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.

Example Find something in a listUse the index and last_index filters to find a specific item in a list.
Liquid
{% var list = "abcabcd" | split %}

index

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

last_index

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

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.

Example Using the reverse filterUse the reverse filter to reverse either a string or a list

When used on null input

Liquid
{{null | reverse | object_type}}
Output
null

When used on a string

Liquid
{{"string" | reverse}}
Output
gnirts

When used on a list

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

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.

Example Sort or shuffle lists with sort, sort_natural, and shuffleUse 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.
Liquid
{%- var stringinputs = 'JucLPXeHBgaZokTRsymNqwFViD' | split: '' -%}
{%- var numberinputs = (1..10) | shuffle -%}
{{- numberinputs | json_encode }}
Output
[7,6,5,9,2,3,8,1,10,4]

Sort strings

Liquid
{{ stringinputs | sort | join: '' }}
Output
BDFHJLNPRTVXZacegikmoqsuwy

The sort filter sorts the strings in ascending order, with capital letters coming before lowercase letters.

Sort and ignore case

Liquid
{{ stringinputs | sort: null, true | join: '' }}
Output
aBcDeFgHiJkLmNoPqRsTuVwXyZ

By passing true as the second argument, the sort filter will ignore capitalization when comparing strings.

Sort and ignore case using sort_natural

Liquid
{{ stringinputs | sort_natural | join: '' }}
Output
aBcDeFgHiJkLmNoPqRsTuVwXyZ

The sort_natural filter is the same as the sort filter with the second argument set to true.

Sort numbers

Liquid
{{ numberinputs | sort | join: ' ' }}
Output
1 2 3 4 5 6 7 8 9 10

The sort filter sorts the numbers in ascending order.

Sort objects

Liquid
{% 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

Liquid
{% 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

Liquid
{{ stringinputs | shuffle | join: '' }}
Output
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

Liquid
{{ stringinputs | shuffle: false | join: '' }}
Output
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

Liquid
{{ numberinputs | sort: 'random' | join }}
Output
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.

Example Getting and manipulating entity listsUse the list methods and filters to create and manage lists of data in your templates
Liquid
{%- 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

Liquid
{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

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

Unique manually

Liquid
{% var houses = houses | uniq %}

Get one random item from the list

Liquid
{% 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

Liquid
{% 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

Liquid
{% 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

Liquid
{%- 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

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

Grouping

Liquid
{%- 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

Liquid
{%- 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 -%}

slice filter

Return a part of the current string or list.

slice: integer start, integer 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.

Example Using the slice filterUse the slice filter to get a portion of a string or list.

When used on null input

Liquid
{{null | slice | object_type}}
Output
null

When used on a string

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

When used on a list

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

With a negative start value

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

With a negative length

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

With a negative start and length

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

With length = 0

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

With a really large negative start

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

With start higher than the input length

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

With a calculated length less or equal to 0

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

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

Liquid
{{ request.date | slice: 2 }}
Output
9/9/2009 12:00:00 AM
Example Getting and manipulating entity listsUse the list methods and filters to create and manage lists of data in your templates
Liquid
{%- 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

Liquid
{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

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

Unique manually

Liquid
{% var houses = houses | uniq %}

Get one random item from the list

Liquid
{% 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

Liquid
{% 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

Liquid
{% 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

Liquid
{%- 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

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

Grouping

Liquid
{%- 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

Liquid
{%- 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 -%}

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 property, boolean 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.

Example Sort or shuffle lists with sort, sort_natural, and shuffleUse 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.
Liquid
{%- var stringinputs = 'JucLPXeHBgaZokTRsymNqwFViD' | split: '' -%}
{%- var numberinputs = (1..10) | shuffle -%}
{{- numberinputs | json_encode }}
Output
[7,6,5,9,2,3,8,1,10,4]

Sort strings

Liquid
{{ stringinputs | sort | join: '' }}
Output
BDFHJLNPRTVXZacegikmoqsuwy

The sort filter sorts the strings in ascending order, with capital letters coming before lowercase letters.

Sort and ignore case

Liquid
{{ stringinputs | sort: null, true | join: '' }}
Output
aBcDeFgHiJkLmNoPqRsTuVwXyZ

By passing true as the second argument, the sort filter will ignore capitalization when comparing strings.

Sort and ignore case using sort_natural

Liquid
{{ stringinputs | sort_natural | join: '' }}
Output
aBcDeFgHiJkLmNoPqRsTuVwXyZ

The sort_natural filter is the same as the sort filter with the second argument set to true.

Sort numbers

Liquid
{{ numberinputs | sort | join: ' ' }}
Output
1 2 3 4 5 6 7 8 9 10

The sort filter sorts the numbers in ascending order.

Sort objects

Liquid
{% 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

Liquid
{% 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

Liquid
{{ stringinputs | shuffle | join: '' }}
Output
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

Liquid
{{ stringinputs | shuffle: false | join: '' }}
Output
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

Liquid
{{ numberinputs | sort: 'random' | join }}
Output
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.

Example Getting and manipulating entity listsUse the list methods and filters to create and manage lists of data in your templates
Liquid
{%- 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

Liquid
{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

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

Unique manually

Liquid
{% var houses = houses | uniq %}

Get one random item from the list

Liquid
{% 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

Liquid
{% 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

Liquid
{% 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

Liquid
{%- 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

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

Grouping

Liquid
{%- 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

Liquid
{%- 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 -%}

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.

Example Sort or shuffle lists with sort, sort_natural, and shuffleUse 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.
Liquid
{%- var stringinputs = 'JucLPXeHBgaZokTRsymNqwFViD' | split: '' -%}
{%- var numberinputs = (1..10) | shuffle -%}
{{- numberinputs | json_encode }}
Output
[7,6,5,9,2,3,8,1,10,4]

Sort strings

Liquid
{{ stringinputs | sort | join: '' }}
Output
BDFHJLNPRTVXZacegikmoqsuwy

The sort filter sorts the strings in ascending order, with capital letters coming before lowercase letters.

Sort and ignore case

Liquid
{{ stringinputs | sort: null, true | join: '' }}
Output
aBcDeFgHiJkLmNoPqRsTuVwXyZ

By passing true as the second argument, the sort filter will ignore capitalization when comparing strings.

Sort and ignore case using sort_natural

Liquid
{{ stringinputs | sort_natural | join: '' }}
Output
aBcDeFgHiJkLmNoPqRsTuVwXyZ

The sort_natural filter is the same as the sort filter with the second argument set to true.

Sort numbers

Liquid
{{ numberinputs | sort | join: ' ' }}
Output
1 2 3 4 5 6 7 8 9 10

The sort filter sorts the numbers in ascending order.

Sort objects

Liquid
{% 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

Liquid
{% 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

Liquid
{{ stringinputs | shuffle | join: '' }}
Output
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

Liquid
{{ stringinputs | shuffle: false | join: '' }}
Output
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

Liquid
{{ numberinputs | sort: 'random' | join }}
Output
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.

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.

Example Using the times filter with a stringYou 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.

Current functionality

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

Future functionality

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

Potential Replacement

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

Alternate Replacement

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

split filter

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

split: string separator
Example Removing duplicates from a listUse the uniq filter to remove duplicate items from a list.
Liquid
{{'bookkeeper' | split: '' | uniq}}
Output
bokepr
Liquid
{{'A B c d a b c' | split: ' ' | uniq}}
Output
ABcdab
Liquid
{{'A B c d a b c' | split: ' ' | uniq: null, true}}
Output
ABcd

Unique by property

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

Unique by property

Liquid
{% var one_per_category = items | unique:'category', true %}
Example Find something in a listUse the index and last_index filters to find a specific item in a list.
Liquid
{% var list = "abcabcd" | split %}

index

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

last_index

Liquid
{{ list | last_index: "a" }}
Output
3
Liquid
{{ list | last_index: "a", 2 }}
Output
0
Liquid
{{ list | last_index: "A" }}
Output
-1
Liquid
{{ list | last_index: "A", -1, true }}
Output
3
Example Using the split filterUse the split filter to convert a string into a list of strings separated by the given separator
Liquid
{{'The quick brown fox jumps over the lazy dog.' | split:" " | json_encode}}
Output
["The","quick","brown","fox","jumps","over","the","lazy","dog."]

Split on multiple characters

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

Does not include empty strings in the result

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

Use an empty pattern to split the string into individual characters

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

where filter

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

where: string property, object value, boolean 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.

Example Filtering listsUse the where and where_exp filters to get a new list containing only the items from the input list that match the provided condition.
Liquid
{% var arr = existing_list | where: 'priority', 1 %}
Liquid
{% var arr = existing_list | where: 'position', 'top', true %}
Liquid
{% var with_rooms = houses | where_exp: 'house', 'house.rooms.value >= 2' %}
Example Getting and manipulating entity listsUse the list methods and filters to create and manage lists of data in your templates
Liquid
{%- 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

Liquid
{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

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

Unique manually

Liquid
{% var houses = houses | uniq %}

Get one random item from the list

Liquid
{% 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

Liquid
{% 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

Liquid
{% 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

Liquid
{%- 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

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

Grouping

Liquid
{%- 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

Liquid
{%- 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 -%}

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 name, string 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.

Example Filtering listsUse the where and where_exp filters to get a new list containing only the items from the input list that match the provided condition.
Liquid
{% var arr = existing_list | where: 'priority', 1 %}
Liquid
{% var arr = existing_list | where: 'position', 'top', true %}
Liquid
{% var with_rooms = houses | where_exp: 'house', 'house.rooms.value >= 2' %}
Example Getting and manipulating entity listsUse the list methods and filters to create and manage lists of data in your templates
Liquid
{%- 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

Liquid
{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

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

Unique manually

Liquid
{% var houses = houses | uniq %}

Get one random item from the list

Liquid
{% 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

Liquid
{% 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

Liquid
{% 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

Liquid
{%- 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

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

Grouping

Liquid
{%- 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

Liquid
{%- 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 -%}