0

i am trying to get every block that has an even position in shopify liquid inside a forloop :

{% for block in section.blocks %}
      {% if forloop.index | modulo : 2 == 0%}
        //some code
      {%endif%}
{% endfor %}

but shopify returns me this error:

Expected end_of_string but found pipe in "forloop.index | modulo : 2 == 0"

Can somone help me solve this? Thanks in advance :D

  • Possibly a duplicate of this question? https://stackoverflow.com/questions/65133100/shopify-step-function-in-liquid-loop-shopify-iteration-with-step/65134077 – Dave B Jan 05 '21 at 14:51

1 Answers1

2

You need to split calculations from logic in liquid.

{% assign num = forloop.index | modulo: 2 %}
{% if num == 0 %}
    // code
{% endif %}

So you must save the module calculation as variable and check it after that, you can't do the check and calculation at the same time.

drip
  • 12,378
  • 2
  • 30
  • 49