Iteration (dealing with lists)
Looping over lists is a very frequent need and in this guide we'll go over the various ways you can do it.
Here are the essential aspects of iteration but you can learn more in the official documentation.
The simplest loop
Let's use the data we defined in an earlier section:
We can loop over the lineItems
array to list our products using a for
loop. It will repeat the code for each item in the array:
The resulting HTML will look like this:
Data formatting
We're formatting the monetary values using the with_delimiter
filter here. You can learn more in our documentation about built-in filters and the PDFMonkey filters.
Learn more
You can learn more about iterating over lists in the official Liquid documentation, including things like limit
, offset
, etc.
Related links
The forloop object
Inside a for
loop, you get access to a special object that stores the current state of the loop. It can be useful to build special cases for the first or last iteration, for instance.
Learn more
Discover the complete range of possibilities of forloop
in the dedicated documentation, including things like index
, index0
or length
.
Last updated