Handlebars - Advanced: How to Conditionally Include Answers

 Note: This feature is a limited release for clients using Version 2 forms. 

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. 


It's a common request to conditionally include questions, depending on their answer.   A typical example is to only include answers that have "Failed" an inspection, so that the viewer of a document can focus on the problems that need to be resolved.


Only Include a Question if it has a Certain Answer

In the below example, we will only include a question and its answer on the document if its answer is "Fail."

Handlebars and HTML Example:

{{#pf:if answers.Carpets.values.[0] "is" "Fail"}}
Are the carpets clean?    {{answers.Carpets.[0]}}



Include All Answers in a Section that have a Certain Answer

This example re-uses the example from this article on printing out all answers in a section into a table.  We recommend becoming familiar with that example first, as this section focuses only on the formatting.

In this scenario, we will only print the question& answer in a table row if the question is answered "Fail".  

Handlebars and HTML Sample

Note:  This sample works for questions with simple, single property answers, like text fields or dropdowns.  Refer to our Basic References article to learn about referencing questions with other data types -- for example, photos need to have their "values" checked and printed out in a specific way.


<!-- Below are the column headers -->

<!-- This "each" is the beginning of the loop -->
{{#each dataRecord.pages.FacilityInspection.sections.CommonArea.answers}}

{{#pf:if values.[0] "is" "Fail"}}
<!-- Below is a table row and cells, with the references (starting AFTER the answers container)-->
{{question}} <!--The question text -->
{{values.[0]}}  <!--The question's answer -->

<!-- This is the end of the loop-->



Example Document Output:


How it works

The #each helper loops through all the answers in the section.  When it reaches the first question, it tests if its value is "Fail" -- if yes, it prints out a table row for it.  If not, it will print no table row.  Then, it moves on to the next question and does the same test, until there are no more questions left in the section. 

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request