Skip to documentation content

Working With Lists

Working with Lists

The list type: range markup, looping, size, and how strings differ from real collections.

Lists are objects that may contain multiple values, and which can be iterated, enumerated, or looped by the template. A list of integers may be created using range markup: (start..end). For example, (1..5) creates a list of numbers from 1 to 5.

Most lists have a common type for all items, although not all lists are limited to a specific type — particularly lists that have been manipulated using filters and methods. Numerous object types may be treated as both a complex object and a list.

Strings are technically both a list and not a list. They can be enumerated with {% for %}, in which case each value is a single character. For most other purposes they are treated as a single value, so {% if "string" is_list %} evaluates to false.

A collection is a value you can loop with {% for %}, that reports how many items it holds (size), and that often exposes pagination metadata when it came from the CMS (total_count, page, limit, start).

Iterate with {% for %} and the forloop object (Control Flow). Narrow or reshape with List Filters. Fetch CMS lists with List methods, including {% entities %} and {% search %}. Creating and Using Collections walks through fetch, filter, loop, empty results, and pagination.

list

An enumerable collection of zero or more objects. Most lists contain a single object type, but some — especially after filters or methods — mix types. Many complex objects are also lists (for example an article list is both a complex object and a list of article objects).

Example Build a list with range markup, loop it, and compare to a string(start..end) range markup builds a list of integers; size and {% for %} work on any list. Strings can be enumerated with {% for %} too, but is_list is false for a string.
Liquid
{%- assign counters = (1..5) -%}
<p>{{ counters.size }} numbers: {% for n in counters %}{{ n }}{% unless forloop.last %}, {% endunless %}{% endfor %}</p>
{%- var greeting = "Hi" -%}
{%- if greeting is_list -%}
	<p>greeting is a list.</p>
{%- else -%}
	<p>greeting is not a list.</p>
{%- endif -%}
{%- for letter in greeting -%}
	{{ letter }}
{%- endfor -%}

{{ forloop }}

Properties
Properties of {{ forloop }} objects
Name Type Description
name string The name of the current forloop (automatically generated from the for method's variable and collection names).
length number The number of items in the forloop
index number The 1-based index of the current item in the for loop.
index0 number The 0-based index of the current item in the for loop.
rindex number The 1-based index of the current item in the for loop counting from the end to the beginning (the reverse of index).
rindex0 number The 0-based index of the current item in the for loop counting from the end to the beginning (the reverse of index0).
first boolean True if the current item is the first item in the for loop.
last boolean True if the current item is the last item in the for loop.