docs/docs/en/template-print/advanced/conditionals.md
Conditional statements allow you to dynamically control the display or hiding of content in the document based on data values. There are three main ways to write conditions:
All conditions begin with a logical evaluation formatter (e.g., ifEQ, ifGT, etc.), followed by action formatters (such as show, elseShow, drop, keep, etc.).
The logical operators and action formatters supported in conditional statements include:
Logical Operators
Action Formatters
The following sections introduce the detailed syntax, examples, and results for each usage.
{data:condition:show(text)}
{data:condition:show(text):elseShow(alternative text)}
Assume the data is:
{
"val2": 2,
"val5": 5
}
The template is as follows:
val2 = {d.val2:ifGT(3):show('high')}
val2 = {d.val2:ifGT(3):show('high'):elseShow('low')}
val5 = {d.val5:ifGT(3):show('high')}
val2 = 2
val2 = low
val5 = high
Use consecutive condition formatters to build a structure similar to a switch-case:
{data:ifEQ(value1):show(result1):ifEQ(value2):show(result2):elseShow(default result)}
Or achieve the same with the or operator:
{data:ifEQ(value1):show(result1):or(data):ifEQ(value2):show(result2):elseShow(default result)}
Data:
{
"val1": 1,
"val2": 2,
"val3": 3
}
Template:
val1 = {d.val1:ifEQ(1):show(A):ifEQ(2):show(B):elseShow(C)}
val2 = {d.val2:ifEQ(1):show(A):ifEQ(2):show(B):elseShow(C)}
val3 = {d.val3:ifEQ(1):show(A):ifEQ(2):show(B):elseShow(C)}
val1 = A
val2 = B
val3 = C
Use the logical operators and/or to test multiple variables:
{data1:ifEQ(condition1):and(.data2):ifEQ(condition2):show(result):elseShow(alternative result)}
{data1:ifEQ(condition1):or(.data2):ifEQ(condition2):show(result):elseShow(alternative result)}
Data:
{
"val2": 2,
"val5": 5
}
Template:
and = {d.val2:ifEQ(1):and(.val5):ifEQ(5):show(OK):elseShow(KO)}
or = {d.val2:ifEQ(1):or(.val5):ifEQ(5):show(OK):elseShow(KO)}
and = KO
or = OK
In the following sections, the described formatters use the inline condition syntax with the following format:
{data:formatter(parameter):show(text):elseShow(alternative text)}
{data:ifEQ(value):and(new data or condition):ifGT(another value):show(text):elseShow(alternative text)}
{d.car:ifEQ('delorean'):and(.speed):ifGT(80):show('TravelInTime'):elseShow('StayHere')}
If d.car equals 'delorean' and d.speed is greater than 80, the output is TravelInTime; otherwise, the output is StayHere.
{data:ifEQ(value):or(new data or condition):ifGT(another value):show(text):elseShow(alternative text)}
{d.car:ifEQ('delorean'):or(.speed):ifGT(80):show('TravelInTime'):elseShow('StayHere')}
If d.car equals 'delorean' or d.speed is greater than 80, the output is TravelInTime; otherwise, the output is StayHere.
{data:ifEM():show(text):elseShow(alternative text)}
null:ifEM():show('Result true'):elseShow('Result false')
[]:ifEM():show('Result true'):elseShow('Result false')
For null or an empty array, the output is Result true; otherwise, it is Result false.
{data:ifNEM():show(text):elseShow(alternative text)}
0:ifNEM():show('Result true'):elseShow('Result false')
'homer':ifNEM():show('Result true'):elseShow('Result false')
For non-empty data (such as the number 0 or the string 'homer'), the output is Result true; for empty data, the output is Result false.
{data:ifEQ(value):show(text):elseShow(alternative text)}
100:ifEQ(100):show('Result true'):elseShow('Result false')
'homer':ifEQ('homer'):show('Result true'):elseShow('Result false')
If the data equals the specified value, the output is Result true; otherwise, it is Result false.
{data:ifNE(value):show(text):elseShow(alternative text)}
100:ifNE(100):show('Result true'):elseShow('Result false')
100:ifNE(101):show('Result true'):elseShow('Result false')
The first example outputs Result false, while the second example outputs Result true.
{data:ifGT(value):show(text):elseShow(alternative text)}
1234:ifGT(1):show('Result true'):elseShow('Result false')
-23:ifGT(19):show('Result true'):elseShow('Result false')
The first example outputs Result true, and the second outputs Result false.
{data:ifGTE(value):show(text):elseShow(alternative text)}
50:ifGTE(-29):show('Result true'):elseShow('Result false')
1:ifGTE(768):show('Result true'):elseShow('Result false')
The first example outputs Result true, while the second outputs Result false.
{data:ifLT(value):show(text):elseShow(alternative text)}
-23:ifLT(19):show('Result true'):elseShow('Result false')
1290:ifLT(768):show('Result true'):elseShow('Result false')
The first example outputs Result true, and the second outputs Result false.
{data:ifLTE(value):show(text):elseShow(alternative text)}
5:ifLTE(5):show('Result true'):elseShow('Result false')
1290:ifLTE(768):show('Result true'):elseShow('Result false')
The first example outputs Result true, and the second outputs Result false.
{data:ifIN(value):show(text):elseShow(alternative text)}
'car is broken':ifIN('is'):show('Result true'):elseShow('Result false')
[1,2,'toto']:ifIN(2):show('Result true'):elseShow('Result false')
Both examples output Result true (because the string contains 'is', and the array contains 2).
{data:ifNIN(value):show(text):elseShow(alternative text)}
'car is broken':ifNIN('is'):show('Result true'):elseShow('Result false')
[1,2,'toto']:ifNIN(2):show('Result true'):elseShow('Result false')
The first example outputs Result false (because the string contains 'is'), and the second example outputs Result false (because the array contains 2).
{data:ifTE('type'):show(text):elseShow(alternative text)}
'homer':ifTE('string'):show('Result true'):elseShow('Result false')
10.5:ifTE('number'):show('Result true'):elseShow('Result false')
The first example outputs Result true (since 'homer' is a string), and the second outputs Result true (since 10.5 is a number).
Conditional blocks are used to display or hide a section of the document, typically to enclose multiple tags or an entire block of text.
{data:ifEQ(condition):showBegin}
Document block content
{data:showEnd}
Data:
{
"toBuy": true
}
Template:
Banana{d.toBuy:ifEQ(true):showBegin}
Apple
Pineapple
{d.toBuy:showEnd}Grapes
When the condition is met, the content in between is displayed:
Banana
Apple
Pineapple
Grapes
{data:ifEQ(condition):hideBegin}
Document block content
{data:hideEnd}
Data:
{
"toBuy": true
}
Template:
Banana{d.toBuy:ifEQ(true):hideBegin}
Apple
Pineapple
{d.toBuy:hideEnd}Grapes
When the condition is met, the content in between is hidden, resulting in:
Banana
Grapes