The humble IF statement is the core of all logic in any programming language, HotDocs included. IF statements don’t actually do anything but rather, they provide a structure within which other commands are executed. To bring any intelligence to your document-producing systems, you must understand the IF, ELSE IF and ELSE statements, as they are used everywhere.
I’ll work with a computation for these examples, as it is simpler than the template version, as no chevrons are required. Lets get some examples happening. Lets say we have a variable named “Var TE” and Var TE has a value of “Bob”.
Example #1a – the basic IF statement
IF Var TE = "Bob"
"His name is Bob"
END IF
//this would produce "His name is Bob".
Example #1b – the basic IF statement
IF Var TE = "Joe"
"His name is Joe"
END IF
//this would produce an error
The above code produces an error because Var TE does not equal Joe (it’s still Bob) and there is no script in place to handle what happens if the variable isn’t equal to “Bob”.
So lets look at how to handle that with an ELSE statement.
Example #2 - IF and ELSE
IF Var TE = "Dave"
"His name is Dave"
ELSE
"His name is not Dave"
END IF
//this would produce "His name is not Dave". In this example - we have some code to run if his name is "Dave", ELSE (readable as "if its anything else in the world..."), produce "His name is not Dave".
This still isn’t ideal. What if we want to do something specific for Bob OR Dave, but something entirely different if it isn’t either of them? Lets look at the ELSE IF statement.
Example #3 – IF, ELSE IF and ELSE
IF Var TE = "Dave"
"His name is Dave"
ELSE IF Var TE = "Bob"
"His name is Bob"
ELSE
"His name is not Bob, nor is it Dave"
END IF
//This would produce "His name is Bob". If we set Var TE to the value of "Mark", the result would have been "His name is not Bob, nor is it Dave"
The easiest way to read this stuff when you’re learning is as follows:
IF (Some specific condition is true)
//do something here, for this condition only
ELSE IF (Some other specific condition is true)
//do something different, for this condition only
ELSE (if its anything else in the whole wide world)
//do something different here if nothing previously is true
END IF (close the statement)
In english, it would read exactly as it is typed above – IF something is true, do something, else if something different is true, do something different for this condition only, else if its anything else in the whole wide world, do something here. It gets tricky, but it is really just a series of “what if this is true” questions and results.
Some things to remember….
1) Every IF statement must have a matching END IF – they are a fixed pair with no exceptions!
2) ELSE and ELSE IF statements can only occur inside an IF/END IF pair. They do not have matching END IFs themselves
3) Nested IF and END IF pairs must nest “inside” each other. I’ll use bracketed numbers in place of conditions to demonstrate:
IF (1)
//do something
IF (2)
//do something here
ELSE
//do something different
END IF(2)
IF (3)
//do something different again
ELSE IF (3)
//do something weird
ELSE (3)
//do something really weird
END IF (3)
END IF(1)
As you can see, IF statements work from the inside out. It is nearly always best practice to write your IF and END IF at the same time, then move your cursor back and complete the rest of the commands between the IF/END IF pair. If you open an IF statement and immediately close it, you will not lose where you are up to with code and save yourself substantial time debugging problematic HotDocs code.
Please note: HotDocs will evaluate your IF statements from top to bottom. In the above example, if IF statement #2 was true AND IF statement #3 was true, HotDocs would only produce #2 – because it will find a true If statement, produce the result and then jump straight to the END IF. Bear this in mind when designing your code.