سؤال

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?

هل كانت مفيدة؟

المحلول

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)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top