Reversing the Loop

You can reverse the direction the loop works with the reversed attribute. To comply with the Ruby lib's functionality, reversedmust be the first attribute.

{% for item in collection reversed %} {{item}} {% endfor %}

Sorting

You can sort the variable with the sorted attribute. This is an extention beyond the scope of Liquid's syntax and thus incompatible but it's useful. The

{% for item in collection sorted %} {{item}} {% endfor %}

If you are sorting a hash, the values are sorted by keys by default. You may decide to sort by values like so:

{% for item in hash sorted:value %} {{item.value}} {% endfor %}

...or make the default obvious with...

{% for item in hash sorted:key %} {{item.key}} {% endfor %}

Numeric Ranges

Instead of looping over an existing collection, you can define a range of numbers to loop through. The range can be defined by both literal and variable numbers:

# if item.quantity is 4...
{% for i in (1..item.quantity) %}
{{ i }}
{% endfor %}
# results in 1,2,3,4

Hashes

To deal with the possibility of looping through hash references, I have chosen to extend the Liquid Engine's functionality. When looping through a hash, each item is made a single key/value pair. The item's actual key and value are in the item.key and item.value variables. ...here's an example: