Handlebars: Conditions (If and Else)

 

Note: This feature is in limited release.  Please contact your account manager. 

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. 

 

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 above or below 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

The #pf:if helper tests if something is true, and shows something based on that.  

Note: This is a customized version of the standard Handlebars "if".  ProntoForms may not support all Handlebars helpers, but the ones listed here are fully supported. 

To write a condition:

  • Open with an #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}} 

[ top ]

 

 

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 any value you are comparing form data to must always be wrapped in quotes.

 

Text

Operator Description Data Example

is

!is

equals (exact match)

does not equal

text, number

{{#pf:if answers.Rating.[0] "is" "Fail"}}

in

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

 [ top ]

 

 

Possible Comparisons

Compare a question to a static value (Note: The static value must always be wrapped in quotes).

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

[ top ]

 

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



[ top ]

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

Comments