In Power Pages (Power Apps Portals), Liquid is used to iterate over collections and display their content. However, errors related to collection iteration may occur when working with lists or collections that aren’t correctly formatted, lack data, or have an invalid structure. Here’s a step-by-step guide to help you resolve collection iteration errors in Liquid.
Common Causes of Collection Iteration Errors
- Empty Collection
- Incorrect Syntax
- Invalid Data Structure
- Nil Collection
- Incorrect Loop Variables
- Incorrect Object Type
- Complex Collection Types
1. Empty Collection
One of the most common issues occurs when the collection you’re trying to iterate over is empty. Liquid does not throw an explicit error for empty collections but will return no output, which can cause confusion.
Example:
{% for item in product_list %}
<p>{{ item.name }}</p>
{% endfor %}
If product_list
is empty, nothing will be rendered, even though no error occurs. This can lead to the perception that there’s a collection iteration error.
Fix: Check whether the collection has data before attempting iteration:
{% if product_list.size > 0 %}
{% for item in product_list %}
<p>{{ item.name }}</p>
{% endfor %}
{% else %}
<p>No products available.</p>
{% endif %}
2. Incorrect Syntax
Using incorrect syntax for iteration can lead to errors. Liquid uses the for
tag for looping through collections, but the loop structure must be correct. Missing {% endfor %}
or improper use of variables can lead to issues.
Example of Incorrect Syntax:
{% for item in product_list %}
<p>{{ item.name }}</p>
{% endif %}
Fix: Ensure that the for
tag is properly closed with {% endfor %}
:
{% for item in product_list %}
<p>{{ item.name }}</p>
{% endfor %}
3. Invalid Data Structure
Liquid expects the collection being iterated over to be in a valid data structure format. If the data being passed to the collection is not in an array or list format, Liquid will not be able to iterate over it.
Example of Invalid Data Structure:
{% for item in "product_list" %}
<p>{{ item.name }}</p>
{% endfor %}
In the above example, "product_list"
is a string, not an actual collection or array.
Fix: Ensure that the collection is properly passed as a list or array and that it’s being accessed correctly.
For example:
{% assign product_list = site.products %}
{% for item in product_list %}
<p>{{ item.name }}</p>
{% endfor %}
4. Nil Collection
When trying to iterate over a collection that is nil
(not defined or null), Liquid may not provide any output, or you may encounter errors in other parts of your code.
Example of Nil Collection:
{% for item in nil %}
<p>{{ item.name }}</p>
{% endfor %}
Since nil
is not a valid collection, this will cause an error.
Fix: Always check if the collection is defined and has data before looping:
{% if product_list %}
{% for item in product_list %}
<p>{{ item.name }}</p>
{% endfor %}
{% else %}
<p>No products available.</p>
{% endif %}
5. Incorrect Loop Variables
Sometimes, errors occur when the loop variable is incorrectly referenced within the loop. For instance, if you mistakenly use an undefined variable inside the loop body, it will lead to an error.
Example of Incorrect Loop Variable:
{% for item in product_list %}
<p>{{ items.name }}</p>
{% endfor %}
In the above code, items.name
is used instead of item.name
.
Fix: Use the correct variable name defined in the for
loop:
{% for item in product_list %}
<p>{{ item.name }}</p>
{% endfor %}
6. Incorrect Object Type
If you try to iterate over a value that is not a collection, such as a string or a single object, Liquid will not be able to iterate over it.
Example of Incorrect Object Type:
{% for item in "product_name" %}
<p>{{ item }}</p>
{% endfor %}
Here, "product_name"
is a string, not a collection, so Liquid will not be able to iterate over it.
Fix: Ensure that you’re iterating over a proper collection (such as an array or list):
{% assign product_names = site.products | map: 'name' %}
{% for item in product_names %}
<p>{{ item }}</p>
{% endfor %}
7. Complex Collection Types
If you’re working with complex collections, such as nested collections or arrays of objects, you may need to adjust the iteration logic. Liquid doesn’t automatically flatten complex data structures, so you may need to use nested loops.
Example of Nested Collections:
{% for category in product_categories %}
<h2>{{ category.name }}</h2>
{% for product in category.products %}
<p>{{ product.name }}</p>
{% endfor %}
{% endfor %}
In this example, category.products
is a nested collection, and you need a nested loop to iterate over the products within each category.
Fix: Use nested for
loops when dealing with nested data structures:
{% for category in product_categories %}
<h2>{{ category.name }}</h2>
{% for product in category.products %}
<p>{{ product.name }}</p>
{% endfor %}
{% endfor %}
Troubleshooting Summary Table
Issue | Cause | Fix |
---|---|---|
Empty collection | Collection has no data | Check if the collection has data before looping |
Incorrect syntax | Missing {% endfor %} or improper loop structure | Correctly close the loop with {% endfor %} |
Invalid data structure | Collection is not in array/list format | Ensure the collection is a valid list/array |
Nil collection | Collection is undefined or null | Check if the collection is defined and has data |
Incorrect loop variable | Typo in loop variable name | Use the correct variable within the loop body |
Incorrect object type | Trying to loop over a string or object | Ensure you’re iterating over a collection |
Complex collection types | Nested or complex data structures | Use nested loops for nested collections |
Best Practices for Collection Iteration in Liquid
- Check for Empty Collections: Always verify that your collection contains data before attempting to iterate.
- Use Correct Syntax: Make sure to close your loops properly and use correct variable names inside the loop.
- Verify Data Structure: Ensure the collection is an array or list, not a single object or string.
- Handle Nil Values: Always check if a collection is defined (not
nil
) before iterating. - Use Nested Loops for Complex Structures: For nested collections, ensure you’re using multiple loops to handle different levels of data.
Example Code to Safely Iterate Over a Collection
{% if product_list.size > 0 %}
{% for item in product_list %}
<p>{{ item.name }}</p>
{% endfor %}
{% else %}
<p>No products available.</p>
{% endif %}
This ensures that you only iterate over product_list
if it has data and provides fallback content when the collection is empty.