List Filters

List Filters

compact filter

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

compact

Examples

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

How to use the compact filter

Copy

Remove null and empty from a list

{%- var mixed = ',,,a,,,b,b,,c,,' | split: ',' -%}
{{- mixed | compact | join: ',' }} <- {{ mixed | join: ',' -}}
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

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

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

Related

concat filter

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

concat: list other

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

Related

first filter

Returns the first item in the input list

first

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

Related

group_by filter

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

group_by: String property

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

Related

{{ group_field }}

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

index filter

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

index: object findInteger startBoolean ignorecase

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

Related

index filter

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

index: String findInteger startBoolean ignorecase

Related

join filter

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

join

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

Related

map filter

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

map: String property

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

Related

{% map %}

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

rand filter

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

rand: Integer lengthBoolean allow_repeatsBoolean prevent_cache

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

Related

size filter

Returns the length of the input string or list.

size

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

Related

uniq filter

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

uniq: String propertyBoolean ignorecase

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

Related

last filter

Returns the last item in the input list

last

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

Related

last_index filter

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

last_index: String findInteger startBoolean ignorecase

Related

last_index filter

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

last_index: object findInteger startBoolean ignorecase

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

Related

reverse filter

Reverses the input string or list.

reverse

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

Related

shuffle filter

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

shuffle: Boolean prevent_cache

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

Related

slice filter

Return a part of the current string or list.

slice: Integer startInteger length

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

Related

sort filter

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

sort: String propertyBoolean ignorecase

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

Related

sort_natural filter

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

sort_natural: String property

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

Related

times filter

Multiply the input by the operand.

times: Number operand

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

Related

{{ time }}

Represents a specific instant in a specific timezone.

split filter

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

split: String separator

Related

where filter

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

where: String propertyobject valueBoolean ignorecase

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

Related

where_exp filter

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

where_exp: String nameString expression

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

Related