if
The if
statement allows for conditional processing of code blocks. In case the given expression is evaluated to false
, an optional else
block is processed instead.
Expressions
Automad understands two types of expressions. You can either test a variable, whether it has a positive value, or compare two operands.
<# Test if variable has a value and is not false. #>
<@ if @{ variable } @>
...
<@ end @>
<# Compare variable to the string 'Test'. #>
<@ if @{ variable } = 'Test' @>
...
<@ end @>
<# Test whether variable is larger than 5. #>
<@ if @{ variable } > 5 @>
...
<@ end @>
if ... else ...
In case an expression evaluates to false
, an optional else
block is processed instead. The syntax for if ... else
looks as follows:
<@ if @{ variable } > 5 @>
...
<@ else @>
...
<@ end @>
Comparison Operators
The following operators can be used to compare two operands of an expression:
=
- Equality - true if both operands have the same value.
!=
- True if both operands are not equal.
>
- Greater than - true if the left operand is greater than the right one.
>=
- Greater or equal - true if the left operand is greater than or equal to the right one.
<
- Smaller than - true in the left operand is smaller than the right one.
<=
- Smaller or equal - true if the left operand is smaller than or equal to the right one.
Logical Operators
The following operators can be used to negate or combine expressions:
!
- True if value is not
true
. The!
can be used to test whether a variable value is either0
or an empty string. not
- Alias for
!
. True if value is nottrue
. and
- Combines two expressions. True if both expressions evaluate to
true
. or
- Combines two expressions. True if one of them evaluates to
true
.
Also multiple combined expressions can be chained to one expression like this:
<@ if not @{ variable1 } and @{ variable2 } = 'Test' or @{ variable3 } > 5 @>
...
<@ end @>
Examples
Simply testing whether a variable is defined and has a value other than 0
or ""
.
<@ if @{ variable } @>
...
<@ end @>
To inverse the test above, use the !
or not
operators:
<# Using "not" #>
<@ if not @{ variable } @>
...
<@ end @>
<# Or alternatively "!" #>
<@ if !@{ variable } @>
...
<@ end @>
Testing values:
<@ if @{ variable | def (1) } >= 5 @>
...
<@ end @>
Combining expressions:
<@ if @{ variable } >= 5 and @{ variable } < 10 @>
...
<@ end @>