Posted in

How to loop through an array in Liquid?

Hey there, fellow Shopify store owners, developers, and Liquid enthusiasts! If you’ve ever spent an hour staring at a messy array of product tags, collection lists, or customer order details in your Shopify theme and thought, “There’s gotta be a better way to work with this,” you’re in the right place. I’ve been in your shoes—for the past 8 years, I’ve run this small, tight-knit Liquid supply business (you know, the kind that ships custom liquid formulations for manufacturing, but hey, we dabble in supporting fellow devs with Liquid know-how too) and nothing’s saved my team more hours than mastering how to loop through arrays in Liquid. Liquid

Let’s cut to the chase: arrays in Liquid are just ordered lists of data, right? They might be product variants, blog posts, order line items, even those random extra fields you added to your customer profiles. If you don’t know how to loop through ’em, you’re stuck copying and pasting code line by line, debugging tiny typos, and wondering why your collection page is showing 1 product when it should show 10. Trust me, I’ve been there. Last year, one of our clients— a small skincare brand using Shopify—reached out because their ingredient page was broken. They had a list of 15 natural ingredients in an array, but their dev had tried to display them without a loop, so it just dumped the raw array text on the frontend. That’s a disaster for UX, and it’s exactly why looping is non-negotiable.

First, let’s start with the most basic loop you’ll use 90% of the time: the for loop. It’s straightforward, works for almost any array, and is the backbone of every Liquid array operation. Let’s say you have an array of your top-performing products from the last quarter—something we use all the time to promote our custom liquid additives for skincare formulations, by the way. The core syntax is simple: you open with {% for item in array %}, add what you want to display, then close it with {% endfor %}. No fancy tricks, just pure Liquid magic. Wait, but let’s make that real. If your array is product.collections or line items from an order, here’s what it looks like in practice: {% for product in collections.all.products %}

{{ product.title }}

{% endfor %}. See? Easy peasy. But hold on—there are little tweaks that make this way more useful, and I’m gonna share those because I’ve learned ’em the hard way.

For example, you don’t always want to show every item in an array. Maybe you only want your top 5 best-selling liquid products (since that’s our bread and butter). The for loop has a limit filter—just add limit: 5 to your opening tag. So that becomes {% for product in collections.all.products limit:5 %}. Boom, no need to slice the array manually, Liquid does it for you. What if you want to skip the first 2 items? Maybe you already featured them in a hero section, so you don’t need to repeat ’em. Add offset:2, like this: {% for product in collections.all.products offset:2 %}. I use this all the time for our own Shopify store—our homepage has a hero for two new liquid formulations, then the next six are pulled from the same array with offset:2. No messy code, just clean, dynamic content.

Wait, what if your array is empty? Oh, I cannot stress how important this is. Nothing’s worse than a blank space on your page where content should be, or a generic “No items found” that feels totally unbranded. The for loop has an else clause that lets you display custom content when the array is empty. Let’s tie that back to our skincare brand client example. If their ingredient array is empty (maybe they forgot to add ingredients to a new product), instead of a broken page, they can do: {% for ingredient in product.ingredients %}

{{ ingredient.name }}

{% else %}

Oops, we’re still mixing up this product’s star ingredients—check back soon!

{% endfor %}. That’s way better than a 404 or blank screen, right? We added that exact fix for that client, and their support tickets about broken product pages dropped by 70% in a month. Worth its weight in gold.

Now, let’s talk about a less obvious loop: the cycle. This isn’t a loop in the same for/for endfor way, but it’s super useful for styling arrays without writing extra CSS. Ever wanted to alternate background colors for a list of items? Like, for our product lines, we do light gray for odd rows and off-white for even rows to make our liquid formulations pop. That’s where cycle comes in. Here’s how it works: inside your for loop, add {% cycle ‘bg-light’, ‘bg-gray’ %}, then apply that class to your item. So: {% for product in collections.all.products %}

{{ product.title }}

{% endfor %}. It cycles through the two classes on every iteration, no extra code needed. I used to write custom CSS classes for every row until I found cycle—saved me so much time on custom theme tweaks.

Wait, what about when you need to access more than just one item at a time? Maybe you have an array of order line items and you need both the product name and the quantity, or a custom liquid blend that has multiple components. The for loop lets you pull multiple attributes per item, which is perfect for our business. For example, if we track every custom liquid order’s formulation components in an array, we can loop through that array like: {% for component in order.formulation.components %}

{{ component.name }}: {{ component.quantity }}ml

{% endfor %}. That’s exactly what we use to send automated order confirmations to clients, so they can see exactly what they’re getting in their custom blend. No manual data entry, no mistakes—Liquid does all the heavy lifting.

But hold on, there are gotchas. I’ve made every single one of these mistakes, so let’s save you the time. First, array indexing: Liquid arrays start at 0, not 1. So if you’re trying to get the first item in an array, it’s array[0], not array[1]. I once messed up a client’s custom liquid quote because I used array[1] instead of array[0], and they got a quote for the second blend instead of the one they requested. Oops. Lesson learned. Second, make sure the array actually exists. If you reference an array that isn’t defined (like a custom field you forgot to add to your theme settings), the loop will just break silently. Always test your loops with sample data first, or add a check like {% if array %} before your loop to avoid errors. Third, don’t overcomplicate things. You don’t need fancy nested loops unless you actually need them. For example, if you have a collection array inside a product array, you can loop through both, but only do it if you need the data—too many nested loops slow down your page load, and slow pages kill conversion rates. Our web dev team runs speed tests every month, and nested loops are one of the first things we cut if page load time is high.

Now, let’s tie this back to why this matters for businesses like mine. I’m not just a random dev giving tips—I’m a supplier of custom liquid formulations, and I work with small businesses every day who rely on Shopify to sell their products. A lot of these businesses don’t have big dev teams, so they’re DIY-ing their websites, and mastering Liquid loops is the difference between a functional site and one that actually drives sales. When a customer lands on our store, they can loop through our formulation specs easily, see our top blends, and get a clear, fast experience—no confusing code, no broken pages. That’s what keeps customers coming back to us for their custom liquid needs, not just because our products are good, but because our team knows how to make Shopify work for small brands.

Wait, let’s make sure you’re not missing any key loop types. There’s also the limitless for loop, but that’s rarely used because it just runs until the array is done. There’s the tablerow loop, which is specifically for making HTML tables—super useful if you’re looping through a product array and want to display them in columns, not rows. For example, if you have a 4-column grid of custom liquid samples, tablerow is perfect: {% tablerow product in collections.samples.products cols:4 %} {% endtablerow %}. That will automatically arrange the items into 4 columns, no need for extra grid CSS. I used this for our sample page last quarter, and it cut the CSS work by an hour. Game changer.

Another pro tip: if you’re working with dynamic arrays (like data from a customer’s order or a form submission), you can combine loops with other Liquid filters. Want to sort your product array by price? Add sort: ‘price’ to your for loop. So {% for product in collections.all.products sort: ‘price’ %}. Want to reverse the order? Add reversed: sort: ‘price’ reversed. We use this for our pricing page—we show our most affordable liquid additives first, then go up to our premium custom blends, and reversed makes that order easy to maintain without touching the code every time we update prices. No more messy manual sorting, Liquid does it all.

Now, let’s talk about common pain points I see with loops. One big one is when arrays have empty values or nil items. For example, if a product in your array has a missing title, you’ll get a blank spot. You can fix that by adding a default filter, like {{ product.title | default: ‘Unnamed Product’ }}. I add this to every single loop I write now—no more blank lines ruining the layout. Another one is escaping Liquid tags if you’re writing code in a blog post or documentation. If you ever need to show a Liquid loop in text (like how I’m doing here), you have to use raw and endraw tags, otherwise Liquid will try to execute the code instead of displaying it. I learned that the hard way when I accidentally broke our own support page’s code while writing a tutorial.

So, putting this all together: looping through arrays in Liquid is simple, but knowing the little tricks and workarounds is what turns a basic loop into a tool that saves you time and makes your site better. Whether you’re pulling in product data for our custom liquid blends, building a collection page for your own brand, or fixing a broken order confirmation, mastering these loops is non-negotiable for anyone working with Shopify.

At the end of the day, that’s why I started this business: to help small manufacturers and brands (like those making skincare, cleaning products, or whatever custom liquid they need) not just get high-quality formulations, but also understand the tech side of selling online. I’ve seen too many great products get lost because their owners struggle with website code, and Liquid loops are one of the easiest ways to fix that.

If you’re a brand that needs reliable custom liquid formulations, or you’re stuck on Liquid arrays and loops and need a hand, hit us up to talk through your needs. We don’t just supply materials—we partner with small businesses to make their operations smooth, both in manufacturing and online. Let’s build something that works for you.

Powders References:

  • Shopify Liquid Documentation
  • Shopify Theme Development Guide
  • Liquid for Shopify Developers

Shenzhen Xinweitai Biotechnology Ltd.
As one of the most experienced liquid manufacturers and suppliers in China, we have world-leading production equipment and strong manufacturing capabilities. Please rest assured to buy bulk high quality liquid made in China here from our factory. Customized orders are welcome.
Address: 5A15, Shenzhou Computer Building, Madame Curie Avenue, Vanke City Community, Banqiao Subdistrict, Longgang District, Shenzhen
E-mail: info@vanayt.com
WebSite: https://www.vanayt.com/