There are 2 new advanced options for templating word documents with Device Magic.

  • If statements
  • For loops

If statments

If statements are used for optionally showing content.

The basic syntax is:

{% if [check] %}


{% endif %}

You can use this to hide a section of your word document if a field is blank.


In either case if fields.Name is not present the whole section will not be shown.

At the moment you can:

  • use just the variable to check if it exists
  • use == to check if it matches some value
  • use != to check that is doesn't match a value
  • use >, >=, <, <= to compare numbers
  • use "contains" to check if a string is included in the value
  • use boolean operators "and" and "or"


Note: You have to put quotation marks around text you want to compare to your fields - e.g. {% if == "Bob" %}

Numbers must not have quote marks. e.g. {% if fields.age > 35 %}, {% if fields.rating == 10 %}

Yes/No questions should be compared to "yes" and "no" - e.g. {% if fields.Yes_No_Question == "yes" %}

You can also use if statements across table rows - to do this you just put the start of the If in the first 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 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.

That way if fields.ShipChecked was false or blank the entire row would be hidden.

For Loops


For loops are used when you want to customise the layout of repeat group answers.

The syntax looks like

{% for [variable] in [repeat_group] %}


{% endfor %}

You use whatever you set the [variable] to, to access the information for each item in the repeat group.


- The [variable] can be any name (preferably something unique)

- [repeat_group] is the identifier of the group itself, e.g. fields.Subgroup.RepeatGroup



Creating a table using a for loop is a special case.

To create a table with a for loop, make a table with a Header row and a Template row.

Put the {% for [variable] in [repeat_group] %} placeholder at the beginning of the first cell and then {% endfor %} at the end of the last cell.

You can then use the variable as you did in the above example in your table cells.


This will create a new row for each employee filling in the cells as you templated them.

Nested Loops:

It is possible to have repeat groups inside repeat groups. You just follow the syntax as with the previous examples.


Or using the table from before:

For information on quickly getting a copy of valid For loop placeholders for repeat groups in your form, please have a look at this article.

Did this answer your question?