Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate over arrays in liquid templates

I know I can iterate over arrays in liquid template with this code:

{% for item in myarray %}
    <p>{{ item.label }}</p>

But how can I get the index of my item within the array?

like image 776
Stefano Ortisi Avatar asked Mar 20 '14 16:03

Stefano Ortisi


1 Answers

According to the "Liquid for Designers" section on liquid's github...

forloop.length      # => length of the entire for loop
forloop.index       # => index of the current iteration
forloop.index0      # => index of the current iteration (zero based)
forloop.rindex      # => how many items are still left?
forloop.rindex0     # => how many items are still left? (zero based)
forloop.first       # => is this the first iteration?
forloop.last        # => is this the last iteration?

All of these are helper variables for for loops. You will find these two most useful...

forloop.index       # => index of the current iteration
forloop.index0      # => index of the current iteration (zero based)
like image 54
Seth Avatar answered Sep 29 '22 09:09

Seth