Handlebars - Advanced: How to Print all Repeatable Section Entries in a Table

This article is about using Handlebars to reference questions in custom PDF, Word, and HTML documents. We recommend reading about Basic Question References and Advanced Question References before trying this. 

Available on the Advanced and Enterprise tiers:

Essentials
Advanced
Enterprise

About

Repeatable sections allow you to add multiple entries for the same set of questions, as many times as a user needs to. In documents, Repeatable sections are typically printed out in a table. This article explains how to do this most efficiently.

We will print out the below example into a document from a simple Repeatable SectionClosed A Repeatable Section is a subform that contains a set of related questions. The data captured is “repeating”, because the field user can complete the same subform more than once, which creates multiple entries.

Example Form

Here is a form with a Repeatable Section (which collects Today's Date, and the Hours Worked). Only one entry in the repeatable section is shown here to save on space. 

One "row" in the above data structure is one repeatable section entry. You will want to loop through the rows to print them all. In the example below, we will print each "row" in the repeatable section into its own row in the table. This will work whether there are one or many entries in the repeatable section.

HTML and Handlebars Example:

<table>

<!-- Below are the column headers -->
<thead>
<tr>
<th>Date</th>
<th>Hours</th>
</tr>
</thead>

<tbody>
<!-- This "each" is the beginning of the loop -->
{{#each dataRecord.pages.HoursWorked.sections.Timesheet.rows}}

<!-- Below is a table row and cells, with the question references (starting AFTER the rows container)-->
<tr>
<td>{{pages.Timesheet.sections.TimesheetSection.answers.TodaysDate.values.[0]}}</td>
<td>{{pages.Timesheet.sections.TimesheetSection.answers.Hoursworked.values.[0].display}}</td>
</tr>

<!-- This is the end of the loop-->
{{/each}}

</tbody>
</table>

Document Output Example:

How does it work? 

The #each helper tells the document, for each "row" that it finds in the Repeatable section, to print out a table row containing the two cells with the question references. When it is done with one "row", it loops back and starts work on the next row, until all rows have been printed.