Repeating Item Numbers
Count the Items in a List of Repeating Items
Want to display the number of items entered? Use this:
{{ ItemName.number() }}
You can also set conditions based on whether there are more or less than a certain number of items, like this (more on this here).
{% if ItemName.number() > 0 %}
Refer to a Specific Numbered Item in the List
Want to reference a specific item in the list? Use this:
{{ ItemName[0].ItemAttribute }}
Remember that the first item will be zero (0), the second item will be 1, and so on. For example, the following will give you the names of the first and third children:
{{ children[0].firstname }} and {{ children[2].firstname }}
Refer to the Item Number (item 1, item 2, item 3, etc.)
You can also refer to which number that item is in the list using "loop.index". For example:
{% for item in children %} {{ item.childname }} is my child number {{ loop.index }}{% endfor %}
Yields: Jane is my child number 1. Jill is my child number 2.
Calculating the Sum of a Repeating Item Attribute
Here's a formula for calculating the sum of the value of all items in a list. Make sure the attribute is a number or integer:
{{ repeating_item_sum(ItemName, "item.ItemAttribute") }}
Or, format it as a currency:
{{ currency(repeating_item_sum(ItemName, "item.ItemAttribute")) }}
Example for a list of "assets" with "cost" as an item attribute.
You can sum the costs like this:
{{ repeating_item_sum(assets, "item.cost") }}
More Complex Calculations
Calculate the sum of a calculation that you made for each item in a list of repeating items:{{ repeating_item_sum(ItemName, "item.number1-item.number2") }}
The example above will add number1 and number2 together for each item, and then sum all of them. You can use other operators within this function.
Special Needs: Adding Text Depending on Which Item it is in the List
Sometimes, you may want to add text to a list of repeating items depending on which item it is in the list. For example, if our traditional comma and oxford comma options don't meet your needs, you may want to add any of these:
- If it's not the last repeating item, add some text:
{% if not loop.last %}something{% endif %}
- If it's the second to last item, add the word "and":
{% if loop.revindex == 2 %} and{% endif %}