Syntax for conditionals
You can use conditionals to vary your output narrative depending on variations in your project data. There are two types of conditional: simple and complex. Both are explained in Key Concepts > Conditionals. Conditionals are sometimes called if-else statements.
Studio's Conditional Builder allows you to create simple conditionals in a way that requires no knowledge of conditional syntax. You can also create complex conditionals in the builder, though this requires knowledge of comparison and logical operators.
You may wish to create conditionals — simple or complex — without using the Conditional Builder. This involves writing ATL directly and requires knowledge of the syntax options.
Operators for conditionals
Each condition within a conditional statement returns a Boolean true or false, typically by comparing two values. The following operators determine the type of comparison:
Operator | Syntax | Example |
---|---|---|
GREATER THAN |
|
|
LESS THAN |
|
|
EQUAL TO |
|
|
GREATER THAN OR EQUAL TO |
|
|
LESS THAN OR EQUAL TO |
|
|
NOT EQUAL TO |
|
|
Note
In the table above, expr
could be a single value, a variable, or something more complex like a function call. If you have two NUMERIC variables — e.g. Sales
and Target
— then Sales > Target
tests whether the Sales
value exceeds the Target
value.
In addition, you have logical operators, which combine or negate individual conditions:
Operator | Syntax | Example |
---|---|---|
AND |
|
|
OR |
|
|
NEGATION |
|
|
The AND and OR operators are required to build complex conditionals. Our Key Concepts > Conditionals topic explains complex conditionals in greater detail and provides examples.
The NEGATION operator is often used with ATL functions that return a Boolean true or false. The NEGATION operator flips true to false, and vice versa. Here is an example using the contains function:
ATL in Script | Result |
---|---|
[[ myList = makeList('Peter', 'Paul', 'Mary') nameToFind = 'John' if(!contains(myList, nameToFind) == true){The list does not contain John.} else{The list includes John.} ]] | The list does not contain John. |
Full syntax for conditionals
The syntax for conditionals is:
[[ if(condition){result} elseif(condition){result} else{result} ]] |
A full-syntax conditional always returns a string, and the result — defined between the curly brackets { } — can contain a mixture of fixed text and ATL blocks.
Assume a "Describe Each Row" project with this data:
Code | Branch | Sales | SalesTarget | |
---|---|---|---|---|
Row 1 | 1001 | New York | 302,234.11 | 300,000.00 |
Row 2 | 1002 | London | 297,659.38 | 300,000.00 |
Row 3 | 1003 | Paris | 175,000.00 | 175,000.00 |
Here's an example of a conditional written in full syntax.
[[ if(Sales > SalesTarget){[[Branch]]'s sales were above target.} elseif(Sales < SalesTarget){[[Branch]]'s sales were below target.} else{[[Branch]]'s sales exactly equalLed the target.} ]]
The conditional starts with an if statement that asks if Sales is greater than SalesTarget. If true, the conditional returns a short sentence confirming this. The result goes between curly brackets and can include ATL.
When the if statement is false, the conditional moves to an elseif statement that asks if Sales is less than SalesTarget. If true, the conditional returns a short sentence confirming this.
If the elseif statement is false, the conditional moves to an else statement (the default). If the two previous conditions are false, Sales must exactly equal Sales Target, so the conditional returns a short sentence confirming this.
Important
You can include as many elseif statements as you like, and the else statement is optional. However, be aware that if no given conditions are true and there is no else statement, the output from the conditional is an empty string.
Short syntax for conditionals
Use this syntax when the result for each condition is a single ATL-computed value. Here, each result is a single ATL block (defined by the square brackets), and there are no surrounding curly brackets { }.
[[ if(condition)[[result]] elseif(condition)[[result]] else[[result]] ]] |
Assume a "Describe Each Row" project with this data:
Code | Branch | Sales | SalesTarget | |
---|---|---|---|---|
Row 1 | 1001 | New York | 302,234.11 | 300,000.00 |
Row 2 | 1002 | London | 297,659.38 | 300,000.00 |
Row 3 | 1003 | Paris | 175,000.00 | 175,000.00 |
Here is an example of a conditional using simple syntax:
[[ if(Branch == 'New York')[[currencyFormat(Sales,'USD','¤#,##0')]] elseif(Branch == 'London')[[currencyFormat(Sales,'GBP','¤#,##0')]] else[[currencyFormat(Sales,'EUR','¤#,##0')]] ]]
The conditional starts with an if statement that asks if the Branch value is equal to "New York". If true, it returns the Sales value with USD currency formatting. The result for Row 1 is $302,234.
When the if statement is false, the condition moves to an elseif statement that asks if the Branch value is equal to "London". If true, it returns the Sales value with GBP currency formatting. The result for Row 2 is £297,659.
When the elseif statement is false, the conditional moves to an else statement (the default). This returns the Sales value with EUR currency formatting. The result for Row 3 is €175,000.
Note
Note how the results are not enclosed within curly brackets { }. This is because the result consists solely of an ATL block — in this case a call to the currencyFormat function.
If the result is fixed text interspersed with ATL-computed values, use full syntax instead.
Ternary syntax for conditionals
Ternary syntax allows you to write extremely compact if-else conditionals. The syntax is:
[[ condition ? result if true : result if false ]] |
This syntax is best for simple if-else conditionals that don't require an elseif statement. You can, however, nest ternary statements in a way that creates de facto elseif statements.
Assume a "Describe Each Row" project with this data:
Code | Branch | Sales | SalesTarget | |
---|---|---|---|---|
Row 1 | 1001 | New York | 302,234.11 | 300,000.00 |
Row 2 | 1002 | London | 297,659.38 | 300,000.00 |
Row 3 | 1003 | Paris | 175,000.00 | 175,000.00 |
Here is an example of a conditional written in ternary syntax:
[[Sales > SalesTarget ? "[[Branch]] met its sales target." : "[[Branch]] failed to meet its sales target."]]
The conditional asks if Sales is greater than SalesTarget. If true, it returns a sentence stating that the branch met its sales target. If false, it returns a sentence stating that the branch failed to meet its sales target.
Important
The ?
operator comes after the if statement. The results must be separated by a colon.
As mentioned above, you can nest ternary conditionals. For example:
[[Sales > SalesTarget ? "[[Branch]]'s sales were above target." : (Sales < SalesTarget ? "[[Branch]]'s sales were below target." : "[[Branch]]'s sales exactly equalled the target.")]]
The above ATL is the conditional from the Full syntax section re-written in ternary syntax. Note how the nested ternary conditional is enclosed within round brackets. Failure to do this might result in errors.