If statements can be used to add content to your generated PDF/Word document under defined conditions. 

The basic syntax of an if statement is:

{% if [check] %}

[content]

{% endif %}

For example, this can be useful if you'd like a question label to be omitted from the document, if the associated question isn't answered. 

{% if fields.Name %} My name is {{fields.Name}}. {% endif %}

or

{% if fields.Name %} 
My name is {{fields.Name}}.
{% endif %}


In either case, if an answer to {{fields.Name}}  is not present, the section "My name is {{fields.Name}}." will not be added to the document.

Available Functions and Operators:

  • use just the variable to check if it exists.
{% if fields.Name %} My name is {{fields.Name}}. {% endif %}

  • use == to check if it matches some value
{% if fields.Question == "yes" %} This answer is true. {% endif %}

  • use != to check that is doesn't match a value
{% if fields.Quality != "Great" %} 
Quality was not perfect & needs to be redone.
{% endif %}

  • use >, >=, <, <= to compare numbers
{% if fields.Age < 18 %} You are underage! {% endif %}

  • use "contains" to check if a string is included in the value
{% if fields.Multi_Select contains "Option 1" %} 
This includes Option 1.  {% endif %}

  • use boolean operators "and" and "or"
{% if fields.Name != "Tom" or fields.Name == null %} 

Oh dear! You are not Tom.

{% endif %}

Note on null: You can also use null to check if a question is blank.

{% if fields.Age >= 30 and fields.Age <= 60 %} 

You're 30-60 years old and get a discount!

{% endif %}


Note: 

  • Quotation marks are required around text values you wish to compare - e.g.  {% if fields.Name == "Bob" %}
  • Numbers do not require quotation marks - e.g.  {% if fields.rating == 10 %}
  • Yes/No Questions should be compared to "yes" and "no" - e.g. {% if fields.Yes_No_Question == "yes" %}
  • For steps to obtaining valid placeholders in for your form, please have a look at this article.

Other useful examples:

Tables

You can also use if statements across table rows. To do this you just put the start of the if statement in the first column cell and the {% endif %} in the last cell.

e.g. If you had a row of data about our ship that was checked - to hide the whole row if the data is missing you would put {% if fields.ShipChecked %} in the first cell of the row and then the {% endif %} in the last cell of the row.

If {{fields.ShipChecked}} doesn't have a value, the entire row will not be placed in the document.

Checkboxes

The table below shows an example of how to display checkboxes. 

Under the "Yes" column, an empty checkbox will be displayed if the "no" was selected and a ticked checkbox will be displayed if "yes" was selected for the Question.
Likewise, under the "No" column, an empty checkbox will be displayed if "yes" was selected and a ticked checkbox will be displayed if "no" was selected for the Question.

Images

Just like text and tables, you can also use if statements to conditionally show images.
For example, after selecting the name of a client, the associated client's logo can be shown.
In the example template below are 2 potential client logos. 

Note: Placing the previous {% endif %} and next 'if statement start' right next to each other will eliminate unwanted space in your report. 

For the basics on custom templating, check our this article.

If you have any questions or comments please send us a message at support@devicemagic.com.

Did this answer your question?