Skip to content

Loops (#foreach)

Use #foreach to repeat a block once for every item in a list of data.

{{#foreach <list marker>}}
...repeated for each item...
{{#endforeach}}

Every #foreach must be closed with #endforeach. The marker you loop over must be a list.

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.00

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}}

You can sort the list as you loop. Provide a sort type, the field to sort on, and the direction (asc or desc):

Sort typeUse forExample
sort-byText (A–Z){{#foreach matter.timerecords sort-by feeearner asc}}
numerical-sort-byNumbers{{#foreach matter.timerecords numerical-sort-by amount desc}}
date-sort-byDates{{#foreach matter.timerecords date-sort-by date asc}}

Example — highest value first:

{{#foreach matter.timerecords numerical-sort-by amount desc}}
{{feeearner}}: {{amount}}
{{#endforeach}}

← Conditionals · Contents · Maths →