Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do a loop n.times in Liquid templating using an existing for loop

Tags:

loops

liquid

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!

like image 900
TJ Sherrill Avatar asked Oct 28 '12 22:10

TJ Sherrill


People also ask

How do you break a loop in Shopify liquid?

Class: Liquid::Break Break tag to be used to break out of a for loop.

How do you create an array in a for loop in liquid?

You can directly create a new empty array controllers and concat to it your controllerName converted into an array using the workaround split:'' . The result is directly an array, without the extra string manipulations.

How do you limit a loop in Python?

zip(range(limit), items) Using Python 3, zip and range return iterables, which pipeline the data instead of materializing the data in lists for intermediate steps. To get the same behavior in Python 2, just substitute xrange for range and itertools. izip for zip .


2 Answers

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) %}
like image 147
Justin Niessner Avatar answered Sep 23 '22 17:09

Justin Niessner


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 %}
like image 32
MrMesees Avatar answered Sep 25 '22 17:09

MrMesees