Handlebars - Conditions
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:
Contents
About
When building a custom document, you may want to write conditional statements, and do different things in the document based on whether those conditions are true or not.
For example, you may want to:
- Show a question only if it has an answer that contains, equals, or is less than or greater than a certain value.
- Show a section conditionally, based on the answer to a previous question in a form.
- Conditionally format answers (red for fail, green for pass).
The #pf:if helper
To support existing integrations, some items will continue to use “prontoforms” or “pf” in the domain or code.
For more detailed information about what’s changing, visit https://support.truecontext.com/hc/en-us/articles/19516168513556
The #pf:if helper tests if something is true, and shows something based on that.
To write a condition:
- Open with
#pf:if
and the thing you need to test. - Then, list the data that should be shown if the statement is true.
- Then, close the
#pf:if
with/pf:if
. - Optionally, include an
else
to specify what should happen in other scenarios.
Example
In the example below, the string "Pass" will be shown if the "Score" question's answer is greater than 50. If it is NOT greater than 50, the string "Fail" will be shown.
{{#pf:if answers.Score.[0] ">" "50"}}Pass{{else}}Fail{{/pf:if}}
Supported Operators
You can write a condition that tests any part of a question, section, or page using the following operators.
Note: The operator and comparison values must be wrapped in quotation marks (").
Text
Operator | Description | Data | Example |
is |
equals (exact match) |
text, number |
{{#pf:if answers.Rating.[0] "is" "Fail"}} |
in |
equals one of a list of strings does not equal any of a list of strings |
text, number |
{{#pf:if answers.Rating.[0] "in" "Excellent" "Good"}} |
contains !contains |
contains does not contain |
text | {{#pf:if answers.Comments.[0] "contains" "Poor"}} |
starts-with !starts-with |
starts with does not start with |
text | {{#pf:if answers.Name.[0] "starts-with" "John"}} |
ends-with !ends-with |
ends with does not end with |
text | {{#pf:if answers.Name.[0] "ends-with" "Smith"}} |
matches !matches |
matches a regular expression does not match a regular expression |
text | {{#pf:if answers.ID.[0] "matches" "^(ab)+$"}} |
Numeric Only
Operator | Description | Data | Example |
= != |
equals (exact match) does not equal |
number | {{#pf:if answers.Count.[0] "=" "5"}} |
< | less than | number | {{#pf:if answers.Score.[0] "<" "50"}} |
<= | less than or equal to | number | {{#pf:if answers.Age.[0] "<=" "10"}} |
> | greater than | number | {{#pf:if answers.Score.[0] ">" "50"}} |
>= | greater than or equal to | number | {{#pf:if answers.Age.[0] ">=" "50"}} |
Possible Comparisons
Compare a question to a static value
Note:The static value must always be wrapped in quotation marks (").
{{#pf:if answers.Score.[0] ">" "50"}}Something to display.{{/pf:if}}
Compare a question to another question
{{#pf:if answers.Score.[0] ">" answers.LastScore.[0]}}Something to display.{{/pf:if}}
Basic Examples
Print an answer only if it equals "Fail"
{{#pf:if answers.Rating.[0] "is" "Fail"}}{{answers.Rating.[0]}}{{/pf:if}}
Print out an answer of "1" as "Pass". Otherwise, print "Fail"
{{#pf:if answers.Rating.[0] "=" "1"}}Pass{{else}}Fail{{/pf:if}}