Nested Loops

Nested loops allow you to iterate over arrays within arrays, enabling complex document structures such as grouped recommendations, multi-level lists, and hierarchical data.

Syntax

Place an {{#each}} block inside another {{#each}} block. The inner loop iterates over a property of the current outer item.

Template syntax
{{#each strategy_groups}}
## {{group_name}}

{{#each recommendations}}
- {{title}}: {{description}}
{{/each}}

{{/each}}

Accessing parent context

Inside a nested loop, use {{../field_name}} to access a property from the parent loop's current item.

Parent context access
{{#each clients}}
Client: {{name}}
  {{#each policies}}
  - {{policy_type}} (held by {{../name}}): {{cover_amount}}
  {{/each}}
{{/each}}

Depth limits

While there is no hard limit on nesting depth, deeply nested loops (3+ levels) can make templates difficult to read and maintain. Consider flattening your data structure if you find yourself nesting more than two levels deep.

Example data

Data
{
  "strategy_groups": [
    {
      "group_name": "Superannuation",
      "recommendations": [
        { "title": "Consolidate funds", "description": "Merge existing super accounts." },
        { "title": "Review investment option", "description": "Switch to balanced option." }
      ]
    },
    {
      "group_name": "Insurance",
      "recommendations": [
        { "title": "Increase life cover", "description": "Increase to $1M." },
        { "title": "Add income protection", "description": "75% of income, 2-year benefit." }
      ]
    }
  ]
}