Loops (#foreach)
Use #foreach to repeat a block once for every item in a list of data.
Basic structure
Section titled “Basic structure”{{#foreach <list marker>}} ...repeated for each item...{{#endforeach}}Every #foreach must be closed with #endforeach. The marker you loop over must be a list.
Example
Section titled “Example”matter.timerecords is a list. Each item has fields such as date, feeearner, type, units, and amount. Inside the loop, refer to an item’s fields directly:
{{#foreach matter.timerecords}}- {{type}} by {{feeearner}}: {{amount}}{{#endforeach}}With two time records, this outputs:
- Attendance by A. Solicitor: 350.00- Letter by T. Trainee: 60.00Reaching data from outside the loop
Section titled “Reaching data from outside the loop”Inside a loop, a plain marker refers to the current item. To reach data from outside the loop, prefix it with $parent.:
{{#foreach matter.timerecords}}{{$parent.matter.reference}} — {{type}}: {{amount}}{{#endforeach}}Sorting
Section titled “Sorting”You can sort the list as you loop. Provide a sort type, the field to sort on, and the direction (asc or desc):
| Sort type | Use for | Example |
|---|---|---|
sort-by | Text (A–Z) | {{#foreach matter.timerecords sort-by feeearner asc}} |
numerical-sort-by | Numbers | {{#foreach matter.timerecords numerical-sort-by amount desc}} |
date-sort-by | Dates | {{#foreach matter.timerecords date-sort-by date asc}} |
Example — highest value first:
{{#foreach matter.timerecords numerical-sort-by amount desc}}{{feeearner}}: {{amount}}{{#endforeach}}