Warm tip: This article is reproduced from serverfault.com, please click

Shopify STEP function in liquid loop ? Shopify iteration with STEP

发布于 2020-12-03 19:50:55

Im working in Liquid with a shopify template for the first time and I cant find any documentation on the Liquid loop function for a step object ?

            {% for i in (2..40)  %}                 
              <option value="{{ i }}">{{ i }}</option>
            {% endfor %} 

I simply want to step 2 but have tried a ruby 2.step(40,2) and java and other forms to no avail? Has anyone found this? or is there a modulo check I can use?

PS. I just tried

    {% if i | modulo:2 == 0 %}  

But this threw an error, can't find documentation

Questioner
Steven Moffat
Viewed
0
Dave B 2020-12-04 05:00:17

The important thing to remember is that Liquid is a templating language, not a programming language. There are a lot of limitations to the amount of programming logic that you do inside the liquid tags.

The for loop has a few different options, but step is not one of them. If you want to iterate through the integers 2, 4, 6... 20 the only way I can think to do that is to iterate through the values 1 ... 10 and use {% assign val = forloop.index | times: 2 %} inside the loop to get the values that you want.

Similarly, you are not able to perform any operations within an if or unless tag. You are only allowed to make one or more comparisons, connected with either and or or keywords. You cannot even use parentheses or brackets inside your if or unless statements to group your logic. (Parentheses are legal characters, but they're simply ignored) Instead, you will need to assign the value to a variable, then use that variable to make your comparison.