List Filters
compact filter
Removes all null, empty, and invalid (is_valid == false, empty lists, etc..) objects from the list.
Related
concat filter
Adds all of the items from the other list onto the end of the input list.
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
{% "a" | concat:"b" | json_encode %}
["a", "b"]
Use the list methods and filters to create and manage lists of data in your templates
{%- datastore_items var featured_houses = datastore:"houses" query:"is_featured = true" -%}
{%- datastore_items var houses_by_folder = datastore:"houses" folder:entity.folder -%}
{% datastore_items var houses = featured_houses houses_by_folder unique %}
{% var houses = featured_houses | concat: houses_by_folder %}
{% var houses = houses | uniq %}
{% 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.
{% 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.
{% 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.
{%- 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 -%}
{% var few_rooms = houses | where_exp: "house", "house.rooms.value < 3" %}
{%- 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 -%}
{%- 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
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.
{% var input = 'a,b,c,d' | split:',' %}
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.
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.
{%- 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
{%- datastore_items var featured_houses = datastore:"houses" query:"is_featured = true" -%}
{%- datastore_items var houses_by_folder = datastore:"houses" folder:entity.folder -%}
{% datastore_items var houses = featured_houses houses_by_folder unique %}
{% var houses = featured_houses | concat: houses_by_folder %}
{% var houses = houses | uniq %}
{% 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.
{% 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.
{% 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.
{%- 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 -%}
{% var few_rooms = houses | where_exp: "house", "house.rooms.value < 3" %}
{%- 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 -%}
{%- 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
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.
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.
{% var list = "abcabcd" | split %}
{{ list | index: "A" }}
-1
{{ list | index: "a", 1 }}
3
{{ list | index: "A", 1, true }}
0
{{ 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.
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.
{% var input = 'ABC About' %}
{{input | index: 'A', 1}}
4
{{input | index: 'Ab', 0, true}}
0
{{input | last_index: 'A'}}
4
{{input | last_index: 'A', 3}}
0
{{input | last_index: 'AB'}}
0
{{input | last_index: 'AB', -1, true}}
4
{{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.
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.
{{ "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.
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.
{%- 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
{%- datastore_items var featured_houses = datastore:"houses" query:"is_featured = true" -%}
{%- datastore_items var houses_by_folder = datastore:"houses" folder:entity.folder -%}
{% datastore_items var houses = featured_houses houses_by_folder unique %}
{% var houses = featured_houses | concat: houses_by_folder %}
{% var houses = houses | uniq %}
{% 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.
{% 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.
{% 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.
{%- 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 -%}
{% var few_rooms = houses | where_exp: "house", "house.rooms.value < 3" %}
{%- 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 -%}
{%- 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
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).
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.
{%- 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.
{%- 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.
{%- 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.
{%- 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.
{%- 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.
{%- 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.
{%- 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.
{{ 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.
{{ "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.
{{ "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.
{{ "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.
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
{{"item1,item2,item3" | split:"," | size}}
3
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.
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.
{{'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
{%- blog_posts posts = limit:20 sort_by:'date_posted' sort_direction:'desc' -%}
{%- var latest_by_blog = posts | uniq:'blog_guid' -%}
{% var one_per_category = items | unique:'category', true %}
Use the list methods and filters to create and manage lists of data in your templates
{%- datastore_items var featured_houses = datastore:"houses" query:"is_featured = true" -%}
{%- datastore_items var houses_by_folder = datastore:"houses" folder:entity.folder -%}
{% datastore_items var houses = featured_houses houses_by_folder unique %}
{% var houses = featured_houses | concat: houses_by_folder %}
{% var houses = houses | uniq %}
{% 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.
{% 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.
{% 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.
{%- 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 -%}
{% var few_rooms = houses | where_exp: "house", "house.rooms.value < 3" %}
{%- 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 -%}
{%- 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
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.
{% var input = 'a,b,c,d' | split:',' %}
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.
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.
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.
{% var list = "abcabcd" | split %}
{{ list | index: "A" }}
-1
{{ list | index: "a", 1 }}
3
{{ list | index: "A", 1, true }}
0
{{ 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.
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
{{null | reverse | object_type}}
null
{{"string" | reverse}}
gnirts
{{"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.
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.
{%- var stringinputs = 'JucLPXeHBgaZokTRsymNqwFViD' | split: '' -%}
{%- var numberinputs = (1..10) | shuffle -%}
{{- numberinputs | json_encode }}
[7,6,5,9,2,3,8,1,10,4]
{{ stringinputs | sort | join: '' }}
BDFHJLNPRTVXZacegikmoqsuwy
The sort filter sorts the strings in ascending order, with capital letters coming before lowercase letters.
{{ stringinputs | sort: null, true | join: '' }}
aBcDeFgHiJkLmNoPqRsTuVwXyZ
By passing true as the second argument, the sort filter will ignore capitalization when comparing strings.
{{ stringinputs | sort_natural | join: '' }}
aBcDeFgHiJkLmNoPqRsTuVwXyZ
The sort_natural filter is the same as the sort filter with the second argument set to true.
{{ numberinputs | sort | join: ' ' }}
1 2 3 4 5 6 7 8 9 10
The sort filter sorts the numbers in ascending order.
{% 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.
{% 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.
{{ 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.
{{ 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.
{{ 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
{%- datastore_items var featured_houses = datastore:"houses" query:"is_featured = true" -%}
{%- datastore_items var houses_by_folder = datastore:"houses" folder:entity.folder -%}
{% datastore_items var houses = featured_houses houses_by_folder unique %}
{% var houses = featured_houses | concat: houses_by_folder %}
{% var houses = houses | uniq %}
{% 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.
{% 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.
{% 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.
{%- 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 -%}
{% var few_rooms = houses | where_exp: "house", "house.rooms.value < 3" %}
{%- 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 -%}
{%- 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.
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.
{{null | slice | object_type}}
null
{{"string" | slice: 2}}
ring
{{"string" | slice: 2, 3}}
rin
{{"ab,cd,ef,gh,ij,kl,mn,op,qr,st,uv,wx,yz" | split:"," | slice: 2, 3 | join: " "}}
ef gh ij
{{"string" | slice: -2}}
ng
{{"string" | slice: 2, -1}}
rin
{{"string" | slice: -4, -2}}
ri
{{"string" | slice: -4, 0}}
ring
{{"string" | slice: -20}}
string
[{{"string" | slice: 20}}]
[]
[{{"string" | slice: 2, -4}}]
[]
{{ 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 "random" to sort the list in a random order - which is functionally identical to using the shuffle filter.
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.
{%- var stringinputs = 'JucLPXeHBgaZokTRsymNqwFViD' | split: '' -%}
{%- var numberinputs = (1..10) | shuffle -%}
{{- numberinputs | json_encode }}
[7,6,5,9,2,3,8,1,10,4]
{{ stringinputs | sort | join: '' }}
BDFHJLNPRTVXZacegikmoqsuwy
The sort filter sorts the strings in ascending order, with capital letters coming before lowercase letters.
{{ stringinputs | sort: null, true | join: '' }}
aBcDeFgHiJkLmNoPqRsTuVwXyZ
By passing true as the second argument, the sort filter will ignore capitalization when comparing strings.
{{ stringinputs | sort_natural | join: '' }}
aBcDeFgHiJkLmNoPqRsTuVwXyZ
The sort_natural filter is the same as the sort filter with the second argument set to true.
{{ numberinputs | sort | join: ' ' }}
1 2 3 4 5 6 7 8 9 10
The sort filter sorts the numbers in ascending order.
{% 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.
{% 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.
{{ 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.
{{ 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.
{{ 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
{%- datastore_items var featured_houses = datastore:"houses" query:"is_featured = true" -%}
{%- datastore_items var houses_by_folder = datastore:"houses" folder:entity.folder -%}
{% datastore_items var houses = featured_houses houses_by_folder unique %}
{% var houses = featured_houses | concat: houses_by_folder %}
{% var houses = houses | uniq %}
{% 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.
{% 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.
{% 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.
{%- 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 -%}
{% var few_rooms = houses | where_exp: "house", "house.rooms.value < 3" %}
{%- 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 -%}
{%- 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 "random" 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.
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.
{%- var stringinputs = 'JucLPXeHBgaZokTRsymNqwFViD' | split: '' -%}
{%- var numberinputs = (1..10) | shuffle -%}
{{- numberinputs | json_encode }}
[7,6,5,9,2,3,8,1,10,4]
{{ stringinputs | sort | join: '' }}
BDFHJLNPRTVXZacegikmoqsuwy
The sort filter sorts the strings in ascending order, with capital letters coming before lowercase letters.
{{ stringinputs | sort: null, true | join: '' }}
aBcDeFgHiJkLmNoPqRsTuVwXyZ
By passing true as the second argument, the sort filter will ignore capitalization when comparing strings.
{{ stringinputs | sort_natural | join: '' }}
aBcDeFgHiJkLmNoPqRsTuVwXyZ
The sort_natural filter is the same as the sort filter with the second argument set to true.
{{ numberinputs | sort | join: ' ' }}
1 2 3 4 5 6 7 8 9 10
The sort filter sorts the numbers in ascending order.
{% 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.
{% 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.
{{ 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.
{{ 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.
{{ 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.
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
{{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.
{{"string" | times:2 | json_encode}}
["string","string"]
{{"string" | times:2 | json_encode}}
Liquid error
{%- map string for i in (1..2) %}string{% endmap -%}
{{-string | json_encode}}
["string","string"]
{%- capture string %}{% for i in (1..2) %}string{% unless forloop.last %},{% endunless %}{% endfor %}{% endcapture -%}
{{-string | split:',' | json_encode}}
["string","string"]
Related
Represents a specific instant in a specific timezone.
split filter
Split a string into a list of substrings separated by the given separator
Examples
Use the split filter to convert a string into a list of strings separated by the given separator
{{'The quick brown fox jumps over the lazy dog.' | split:" " | json_encode}}
["The","quick","brown","fox","jumps","over","the","lazy","dog."]
{{'She sells sea shells on the sea shore.' | split:"sea" | json_encode}}
["She sells "," shells on the "," shore."]
{{"110100101" | split:'1' | json_encode}}
["0","00","0" | split:'1'}}
{{"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.
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.
{% 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
{%- datastore_items var featured_houses = datastore:"houses" query:"is_featured = true" -%}
{%- datastore_items var houses_by_folder = datastore:"houses" folder:entity.folder -%}
{% datastore_items var houses = featured_houses houses_by_folder unique %}
{% var houses = featured_houses | concat: houses_by_folder %}
{% var houses = houses | uniq %}
{% 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.
{% 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.
{% 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.
{%- 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 -%}
{% var few_rooms = houses | where_exp: "house", "house.rooms.value < 3" %}
{%- 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 -%}
{%- 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.
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.
{% 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
{%- datastore_items var featured_houses = datastore:"houses" query:"is_featured = true" -%}
{%- datastore_items var houses_by_folder = datastore:"houses" folder:entity.folder -%}
{% datastore_items var houses = featured_houses houses_by_folder unique %}
{% var houses = featured_houses | concat: houses_by_folder %}
{% var houses = houses | uniq %}
{% 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.
{% 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.
{% 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.
{%- 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 -%}
{% var few_rooms = houses | where_exp: "house", "house.rooms.value < 3" %}
{%- 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 -%}
{%- 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