List Filters

List Filters

index filter

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

index: object findInteger startBoolean ignorecase

Examples

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

Find something in a list

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

index

{{ list | index: "a" }}
{{ list | index: "c" }}
{{ list | index: "A" }}
{{ list | index: "a", 1 }}
{{ list | index: "A", 1, true }}

last_index

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

join filter

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

join

Examples

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

Join a list into a single string

Copy
{{ "abcdefg" | split | join }}
{{ "abcdefg" | split | join:',' }}
{% var list = null %} {% for i in (1..5) %} {% set list = list | concat: i %} {% endfor %} {{ list | join: ' and ' }}
{% templates templates = path:'/examples/dynamically_included' sort_by: 'name' sort_direction:'desc' %}
{{ templates | join: '
' }}

size filter

Returns the length of the input string or list.

size

Examples

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

Using the size filter

Copy

When used on null input

{{null | size}}

When used on a string

{{"string" | size}}

When used on a list

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

When used on anything else

{{request.date | size}}

uniq filter

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

uniq: String propertyBoolean ignorecase

Examples

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

Removing duplicates from a list

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

Unique by property

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

Unique by property

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

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

Getting and manipulating entity lists

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

Concatenate and uniq the easy way

{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

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

Unique manually

{% var houses = houses | uniq %}

Get one random item from the list

{% var random_house = houses | rand %}

Sort the full list randomly

{% set houses = houses | shuffle %}

Various ways to filter and slice

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

Advanced filter

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

Grouping

{% var grouped_by_rooms = houses | group_by: "rooms" | sort: "Key" %} {% for list in grouped_by_rooms %}

{{list.Key | default: "Unknown"}} Rooms ({{list.Value | size }} houses)

{% endfor %}

Mapping and Compact

{% var mapped_by_rooms = grouped_by_rooms | map: "Value" %} {% for list in mapped_by_rooms %}

{{list[0].rooms | default: "Unknown"}} Rooms

  • {{list | map: "description" | compact | join: "
  • " }}
{% endfor %}

first filter

Returns the first item in the input list

first

Examples

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

Getting a specific item from a list

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

first

{{input | first}}

last

{{input | last}}

square bracket

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

last filter

Returns the last item in the input list

last

Examples

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

Getting a specific item from a list

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

first

{{input | first}}

last

{{input | last}}

square bracket

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

reverse filter

Reverses the input string or list.

reverse

Examples

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

Using the reverse filter

Copy

When used on null input

{{null | reverse | object_type}}

When used on a string

{{"string" | reverse}}

When used on a list

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

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 identicial 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

Examples

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

Sorting lists

Copy
{% var stringinputs = 'JucLPXeHBgaZokTRsymNqwFViD' | split: '' %} {% var numberinputs = (1..10) | shuffle %} {{ numberinputs | json_encode }}

Sort strings

{{ stringinputs | sort }}
{{ stringinputs | sort: null, true }}
{{ stringinputs | sort_natural }}

Sort numbers

{{ numberinputs | sort | join }}

Sort objects

{% var sorted = houses | sort: 'number_of_rooms' %}
{% var sorted = houses | sort: 'name', true %}

Sort randomly

{{ stringinputs | shuffle | join: '' }}
{{ numberinputs | sort: 'random' | join }}

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 identicial to using the shuffle filter.

sort: String propertyBoolean ignorecase

Examples

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

Sorting lists

Copy
{% var stringinputs = 'JucLPXeHBgaZokTRsymNqwFViD' | split: '' %} {% var numberinputs = (1..10) | shuffle %} {{ numberinputs | json_encode }}

Sort strings

{{ stringinputs | sort }}
{{ stringinputs | sort: null, true }}
{{ stringinputs | sort_natural }}

Sort numbers

{{ numberinputs | sort | join }}

Sort objects

{% var sorted = houses | sort: 'number_of_rooms' %}
{% var sorted = houses | sort: 'name', true %}

Sort randomly

{{ stringinputs | shuffle | join: '' }}
{{ numberinputs | sort: 'random' | join }}

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

Getting and manipulating entity lists

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

Concatenate and uniq the easy way

{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

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

Unique manually

{% var houses = houses | uniq %}

Get one random item from the list

{% var random_house = houses | rand %}

Sort the full list randomly

{% set houses = houses | shuffle %}

Various ways to filter and slice

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

Advanced filter

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

Grouping

{% var grouped_by_rooms = houses | group_by: "rooms" | sort: "Key" %} {% for list in grouped_by_rooms %}

{{list.Key | default: "Unknown"}} Rooms ({{list.Value | size }} houses)

{% endfor %}

Mapping and Compact

{% var mapped_by_rooms = grouped_by_rooms | map: "Value" %} {% for list in mapped_by_rooms %}

{{list[0].rooms | default: "Unknown"}} Rooms

  • {{list | map: "description" | compact | join: "
  • " }}
{% endfor %}

split filter

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

split: String separator

Examples

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

Using the split filter

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

Split on multiple characters

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

Does not include empty strings in the result

{{"110100101" | split:'1' | json_encode}}

Use an empty pattern to split the string into individual characters

{{"abcdef" | split:'' | json_encode}}

map filter

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

map: String property

Examples

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

Map a list of blog posts to their linked titles

Copy
{% blog_posts posts = limit: 10 sort_by 'post_date' sort_direction:'desc' %} {{ posts | map: 'linked_title' | join: '
' }}

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

Getting and manipulating entity lists

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

Concatenate and uniq the easy way

{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

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

Unique manually

{% var houses = houses | uniq %}

Get one random item from the list

{% var random_house = houses | rand %}

Sort the full list randomly

{% set houses = houses | shuffle %}

Various ways to filter and slice

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

Advanced filter

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

Grouping

{% var grouped_by_rooms = houses | group_by: "rooms" | sort: "Key" %} {% for list in grouped_by_rooms %}

{{list.Key | default: "Unknown"}} Rooms ({{list.Value | size }} houses)

{% endfor %}

Mapping and Compact

{% var mapped_by_rooms = grouped_by_rooms | map: "Value" %} {% for list in mapped_by_rooms %}

{{list[0].rooms | default: "Unknown"}} Rooms

  • {{list | map: "description" | compact | join: "
  • " }}
{% 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

Examples

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

Group blog posts by blog

Copy
{% blog_posts posts = limit: 40 start: 1 sort_by: 'date_posted' sort_direction: 'desc' %} {% var grouped_by_blog = posts | group_by: 'blog' %} {% for group in grouped_by_blog %}

{{ group.Key.title }}: {{ group.Value | size }}

{% endfor %}

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

Getting and manipulating entity lists

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

Concatenate and uniq the easy way

{% datastore_items var houses = featured_houses houses_by_folder unique %}

Concatenate manually

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

Unique manually

{% var houses = houses | uniq %}

Get one random item from the list

{% var random_house = houses | rand %}

Sort the full list randomly

{% set houses = houses | shuffle %}

Various ways to filter and slice

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

Advanced filter

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

Grouping

{% var grouped_by_rooms = houses | group_by: "rooms" | sort: "Key" %} {% for list in grouped_by_rooms %}

{{list.Key | default: "Unknown"}} Rooms ({{list.Value | size }} houses)

{% endfor %}

Mapping and Compact

{% var mapped_by_rooms = grouped_by_rooms | map: "Value" %} {% for list in mapped_by_rooms %}

{{list[0].rooms | default: "Unknown"}} Rooms

  • {{list | map: "description" | compact | join: "
  • " }}
{% endfor %}