How do a loop n.times in Liquid templating using a

2020-02-28 21:02发布

问题:

In ruby I can do n.times do, is it possible to do this in Liquid markup?

My current loop is: for video in site.posts my goal is to run this loop 2 times. There are currently 4 objects that will be called through the loop but I want 8. I hope this is clear!

回答1:

You should be able to use a for loop with a range (n is the number of iterations):

{% for num in (1..n) %}

In some instances of Shopify Liquid, it may also work to use

{% for num in (1...n) %}


回答2:

If like me you want to have a dynamic number that is not based upon a collection length or in-build var you can use includes and default values with capture to work wonders

{% capture count %}{{include.count|default:16}}{% endcapture %}
{% for num in (1...count) %}
    {{num}}
{% endfor %}


标签: loops liquid