Question

I need to loop through an array of items, alternating between two different background colors as I go. Is there a way to do this in liquid markup?

Was it helpful?

Solution

Let's say items contains ["a", "b", "c", "d", "e"], then something like this:

{% for item in items %}
  {{forloop.index | modulo: 2}} -> {{item}}
{% endfor %}

would produce:

1 -> a
0 -> b
1 -> c
0 -> d
1 -> e
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top