Overview
Apsona's document templates support conditional directives (i.e., if-then-else), so that you can selectively include or exclude content in your document based on data conditions. For example, when creating a Terms and Conditions document, you might want to create different sets of terms based on whether your customer's credit rating. Or when creating a thank-you letter, you might want to include extra content or verbiage to recognize your top donors. Here, we describe how to use this feature. If you are not already familiar with Apsona Document Merge, you can visit our pages about the document generation tool in general, and generating Word and PDF documents in particular.An example
Below is a screen shot of a small fragment in a Word document, illustrating the essentials of this feature.
DayNo
, and if that field has a value 6, it produces the text "Saturday." If not, the code looks for the field Day
, and if that field
has value Sun
, it produces the text "Sunday." Otherwise, it produces the text "a weekday."
How to create a conditional directive
You can view an example animation showing how to create these directives using Word.Structure of the IF directive
The IF directive must be of the very specific formIF templateField operator value
templateField
is a merge field name,
operator
is a comparison operator, and value
is the value with which to compare the merge field.
- The letters IF must be in upper case, followed by the template field, and optionally an operator and value, separated by spaces.
- The
templateField
must include only letters, numerals, colons and underscores, no other characters. Thus spaces, for example, are forbidden in thetemplateField
. But references to aggregation fields are allowed. - The
operator
, if present, must be one of the following six:< less than <= less than or equal = equals != not equal to >= greater than or equal > greater than - The
value
must be either a string surrounded by double quotes or a number.
templateField
in the IF directive will appear among the mappable fields in step 2 of the merge action builder, so that you can map it to a data field in your data source.
Examples of IF directives
IF Amount > 500 | Valid |
IF Stage = "Closed Won" | Valid |
IF hasProject | Valid: tests whether hasProject is true (if a checkbox field) or non-empty (if it is not a checkbox field) |
IF Oppties:Amount:Sum > 0 | Valid: tests whether the sublist named Oppties has its aggregation Sum field positive. (See the
documentation about sublists for more information about aggregation fields.) |
IF Opp Stage = "Closed Lost" | Invalid: embedded space in template variable |
IF partnerLevel = "Gold | Invalid: missing closing quote |
if count < 10 | Invalid: IF is not in upper case |
Special case: Testing for empty fields
When you want to test whether a string or text field is non-empty, it is better to use the construct «IF MY_FIELD
», without the operator
or value, rather than using «IF MY_FIELD != ""
». This is because of the way databases distinguish between null and empty values. Thus,
for example, suppose you have in your document the construct below:
The field MY_FIELD is «IF MY_FIELD» not «ENDIF» empty.If you then use this document with data that contains a value for MY_FIELD, the result would be
The field MY_FIELD is not empty.but if the data has a null or empty value for MY_FIELD, the result would be
The field MY_FIELD is empty.Thus the effect is to inject the word "not" exactly when needed.
Avoiding empty spaces
If you create an IF conditional without an ELSE part on a line by itself, it will produce empty space if the conditional fails. Below is an example to illustrate this point.

Constraints and notes
- Every IF field in the document template must be followed by a matching ENDIF directive. The content between those two fields is included or excluded based on whether the condition of the IF directive is met.
- The ELSE IF and ELSE parts of an IF construct or optional. E.g., you can have code that looks like
IF .... ENDIF
without any ELSE IF or ELSE parts. - The ELSE, ELSE IF and ENDIF directives must all be in upper case.
- For a particular IF, you can include as many ELSE IF directives as you want, as long as they are all between the IF and the corresponding ENDIF. But you can have no more than one ELSE directive for a given IF. Moreover, that one ELSE (if present) must be the last directive for its IF block, i.e., you cannot (for example) have an ELSE IF between an ELSE and its ENDIF.
-
You can include IF directives within IF directives. For example, the following structure is perfectly valid. Notice in this example that the IF Hour condition
is within an ELSE IF part, but has its own ENDIF.
Note also that you can include, within the IF or ELSE parts, not just plain text but merge fields as well. For example, the conditional below includes the CONTACT_NAME and MESSAGE_FROM_CONTACT values only if the MESSAGE_FROM_CONTACT value is available:
- If Apsona recognizes a merge field as one of the four if-then-else directives, that merge field will not appear among the mappable fields. But if a merge field is not recognized, e.g., because that doesn't match the required structure of one of these directives, the merge field will appear in the list of mappable fields in step 2 of the merge action builder. This will be an indication that something is wrong about the directive.
- Conditional directives are not supported for merge fields within sublists.
- Conditional directives are currently only supported with Microsoft Word templates containing Word merge fields. They are not yet supported for the angle-bracket-based syntax used by LibreOffice or Google Document templates.