Repeating Item Conditionals
In the previous section, you learned about how to set up basic Repeating Questions to ask for a list of the same item (e.g., ask for all children or all assets of a client).
Use the Word Add In to Insert Conditions Based on Repeating Items
You can set conditions based on the number of instances of a repeat item and based on your repeat item attributes.
Below, you learn how to set up your text conditional on the number of repeating questions, like so:
Conditional Text Based on a Repeating Question
The standard syntax will be:
- {% if ItemName.number() == 0 %}There are none of the item.{% endif %}
- {% if ItemName.number() > 1 %}There's more than one item{% endif %}
Example if item is children:
{% if children.number() == 1 %}There is one child.{% else %}There is more than one child or no children.{% endif %}
{% if children.number() > 5 %}There are more than 5 children{% endif %}
Conditional Statements Within a List of Repeating Items
{% for item in ItemName %} Some text here. {% if item.ItemAttribute == "MultipleChoice" %} Some other text. {% endif %}{% endfor %}
Example: Show text if a child is female{% for item in Children %}The child's name is {{ item.childname }}. {% if item.childgender == "Female" %}She's a female.{% endif %}{% endfor %}
Example: Show text if the mailing address is not blank{% for item in Address %}{% if item.mailingaddress != "" %}{{ item.mailingaddress }}{% endif %}{% endfor %}
Option with commalist function:{{ commalist(ItemName, "[[ if item.ItemAttribute2 == "Value" ]]Something else here[[ endif ]]") }}
Selecting Only Repeating Items with a Particular Attribute
Use Case: Create a list of items selected from the repeating question based on a particular attribute. For example, to list all children who are female. Here's how:
{% for item in ItemName if item.ItemAttribute == "MultipleChoice" %} {{ item.AnotherItemAttribute }} {% endfor %}
Example:
For example, if you have a Repeating Question for Children but you want to make a list of the Female children separately. Here's how you can do (assuming you used the attribute variable name Gender in your interview).
{% for item in Children if item.Gender == "Female" %} {{ item.FullName }}, {% endfor %}
ADVANCED: Repeating Item to Calculate Whether More Than One of the Items Have a Certain Attribute
Testing for One Attribute
Use Case: You want to show a certain phrase if more than one item in the list has a certain attribute. For example, the repeating item is "children", there is a question about "gender", and you want to determine if more than one child in the list is a female. Here's how:
{% if ItemName|selectattr("ItemAttribute", "equalto", "Choice")|list|length > 1 %}Conditional text if more than one Item has ItemAttribute of "Choice." {% endif %}
Testing for More than One AttributeHave two conditions you want to test for?
{% if ItemName|selectattr("ItemAttribute", "in", ["Choice1", "Choice2"])|list|length > 1 %}
Example:
{% if children|selectattr("childgender", "equalto", "Female")|list|length >= 2 %}Two or more kids are female.{% endif %}
More Special Syntax
The possibilities are endless, and there is much more you can do: