Conditions
Many tools in DocuMold expect a condition:
- The
IFandIFSfunctions - The
@IFand@ELSEIFactions
If you are not yet familiar with these functions and actions, it may be helpful to first read Conditional content which introduces most of them.
This document dives into what can be used as conditions and how to make more complex ones.
What makes a condition
In DocuMold, a condition is really just something that evaluates to either TRUE or FALSE. These
two values are the only ones that can be given to IF and every other tool that expect a condition.
In the tutorials, the conditions almost always used the = operator. For example:
<# @IF(client_country = "Canada") #>
A Canadian
<# @ENDIF #>
But there are many more tools you can use for conditions, we’ll list them shortly.
Since conditions can only return TRUE or FALSE, the explanations below are shortened by
only mentioning one of the 2 cases. There is an unwritten “otherwise, returns the other value”.
More condition operators
Operators are similar to functions. They receive values and provide a new value. The difference is that the values received are the ones on its left and on its right.
Here is a list of the operators you can use as conditions.
First, these operators can be used with any kind of values:
left = right: this isTRUEifleftis equal torightleft <> right: this isTRUEifleftis different fromright
However the following operators are only available when left and right are numbers:
left < right: this isTRUEifleftis smaller thanrightleft > right: this isTRUEifleftis greater thanrightleft <= right: this isTRUEifleftis smaller or equal torightleft >= right: this isTRUEifleftis greater or equal toright
For left and right to be numbers (instead of text containing digits), you must either:
- use the
@NUMBER_QUESTIONto set the variable as a number - write a numeric value directly in the code without quotes, ex:
age >= 18 - use a function which converts to a number:
NUMBER(text_containing_numbers) - use a function which returns a number:
FIND("every", "hello everybody")gives the position of the first text within the second one, which is the number7
Here are examples of some of these different operators:
<# IF(age >= 18, "An adult", "A non-adult") #>
<# @IF(client_country <> "Canada") #>
A non-Canadian
<# @ENDIF #>
Functions as conditions
Some functions return either TRUE or FALSE; they can be used as conditions too. Some examples:
CAN_FIND(text_to_find, in_text): returnsTRUEiftext_to_findis inin_text, case sensitiveCAN_SEARCH(text_to_find, in_text): returnsTRUEiftext_to_findis inin_text, case insensitiveCONTAIN(list, value): returnsTRUEiflistcontains thevalue
Here is an example:
<# @IF(CONTAIN(selected_perks, "wedding")) #>
A wedding is planned on....
<# @ENDIF #>
Combining conditions
You may want to combine multiple conditions. These 2 functions receive as many conditions as you
want and return TRUE or FALSE:
AND(cond1, cond2...): returnsTRUEif every condition isTRUEOR(cond1, cond2...): returnsTRUEif at least one condition isTRUE
You can also invert a condition with this:
NOT(condition): returnsTRUEifconditionisFALSE, returnsFALSEifconditionisTRUE
Full example:
<# @IF(AND(drinks_requested, age < 18)) #>
Sorry, but you are not allowed to drink alcoholic beverages.
<# @ENDIF #>
<# IF(OR(age >= 18, is_with_adult), "Is allowed on the harder tracks") #>
Variables as condition
If a variable’s value is TRUE or FALSE, it can be used as a condition directly.
Some ways this can happen:
@YES_NO_QUESTIONaction: the variable is set to eitherTRUEorFALSE@PICK_ONE_QUESTIONaction: can set the variable to a value of your choice, which can beTRUEorFALSEEx:@PICK_ONE_QUESTION(is_adult, choices: [["Adult", TRUE], ["Child", FALSE]])
Chaining operators
DocuMold allows you to “chain” condition operators. This is a small shortcut when you want to compare multiple values.
sample = dm_sample(%{
<# IF(18 <= age < 65, "Regular adult") #>
}).test_with({age: 70}, %{
}).test_with({age: 19}, %{
Regular adult
})
This condition would be TRUE if age is 18 or higher, but also smaller than 65.
Each operator is applied on the values to its left and right: 18 <= age and age < 65. The final
result is TRUE if each operator resulted in TRUE.
The equivalent without chaining would be:
sample = dm_sample(%{
<# IF(AND(18 <= age, age < 65), "Regular adult") #>
}).test_with({age: 70}, %{
}).test_with({age: 19}, %{
Regular adult
})
Other things to read
The main use for conditions is for conditional content. Here is a tutorial about that: Conditional content.