Skip to main content

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

Comments

2 comments

  • Pierre Lamarche

    I'm using Handlebars to generate JSON custom documents. Site https://handlebarsjs.com/guide/builtin-helpers.html#each mentions there are @first and @last variables available within #each loops. I use @last to detect the last object in an array and not insert a comma after it. However (a) those variables are not mentioned here (b) they are available but in what seems to be a non-standard way.

    I tried http://tryhandlebarsjs.com/ to test my templates against JSON data from a form submission. The following fragment loops through pages, sections and answers to show how Handlebars sees the data, especially @variables


    <pages>  {{! Show labels and names from all pages and sections (XML for readability) }}
    {{#each dataRecord.pages}}
      <page index="{{@index}}" key="{{@key}}" label="{{this.label}}" name="{{this.name}}">
      {{#each this.sections}}
        <section index="{{@index}}" key="{{@key}}" type="{{this.type}}" label="{{this.label}}" name="{{this.name}}" first="{{@first}}" last="{{@last}}">
        {{#each this.answers}}    {{! code for repeatable sections omitted for simplicity }}
          <answer index="{{@index}}" key="{{@key}}" label="{{this.label}}" value="{{this.values.[0]}}"></answer>
        {{/each}}
      </section>
      {{/each}}
    </page>
    {{/each}}

    The <section> lines show the values for @first and @last. This is the first line, showing these variables are boolean and led me to assume we could do stuff like {{#if @last}}.
    <section index="0" key="NewSection" type="Flow" label="New Section" name="FLIGHT_INFO" first="true" last="false">

    But if you run the same template fragment within ProntoForms, you get this:
    <section index="0" key="NewSection" type="Flow" label="New Section" name="FLIGHT_INFO" first="first" last="">

    To test for last iteration in a #each loop, you need this (it inserts comma if not last iteration):
    {{#pf:if @last "!is" "last"}},{{/pf:if}}

    It would be nice to have a reference page from ProntoForms with the differences from the standard Handlebars (if there is such a thing).

     

    0
  • Karen Cann

    Thank you for the suggestion! I will add this item to our documentation backlog. Please feel free to send your feedback directly to documentation@prontoforms.com

    0

Please sign in to leave a comment.