Clarifying @IF
The @IF
action interacts with multiple other actions: @ELSEIF
, @ELSE
and @ENDIF
.
This page aims to clarify how to use these actions together.
Note: the examples use short texts for simplicity. Normally, the IF
function would be preferred for
such short text, while the @IF
action would be used when the content is larger or contains formatting.
Let’s start with this example:
<# @IF(age > 30) #>
Older
<# @ELSE #>
Younger
<# @ENDIF #>
The basic idea of @IF
is that you have a section of content that you want to display or hide based on whether a condition
is respected or not. In this example, the age is either greater than 30, or it isn’t.
The content to hide or show is delimited by the @IF
, @ELSEIF
, @ELSE
and @ENDIF
actions.
The section for when the @IF
’s condition is TRUE
(respected) starts right after the @IF
command and it goes on until the next
@ELSEIF
, @ELSE
or @ENDIF
action.
If you want to display a different section for when the @IF
’s condition is FALSE
(not respected), then that section starts
after the @ELSE
command.
In our example, if age
is greater than 30, we display “Older”, otherwise we display “Younger”.
@ELSE is optional
You might not always have content to display when the condition is FALSE
(not respected). In those situations, omit
the @ELSE
command entirely. For example:
<# @IF(age > 30) #>
Older
<# @ENDIF #>
So if the condition doesn’t match, then nothing gets displayed.
Nesting @IF
You can nest a @IF
(and the associated @ELSEIF
, @ELSE
, @ENDIF
) inside any part of another @IF.
<# @IF(age > 30) #>
Older <# @IF(retired) #>and retired<# @ENDIF #>
<# @ELSE #>
Younger <# @IF(at_school) #>and still at school<# @ENDIF #>
<# @ENDIF #>
In that case, the commands following the inner @IF
are used by the inner @IF
until the @ENDIF
is reached,
at which points, commands will be associated with the outer @IF
.
Note: Again, for such short content, using IF
functions would normally be simpler than the @IF
actions, but this
is an example of @IF
.
A shortcut: @ELSEIF
Consider this example:
<# @IF(age > 30) #>
Older
<# @ELSE #>
<# @IF(age > 20) #>
In the middle
<# @ELSE #>
Younger
<# @ENDIF #>
<# @ENDIF #>
Now, if the age is not greater than 30, then there is a second condition. We display “In the middle” if the age is greater than 20, “Younger” otherwise.
Only having a nested @IF
in a @ELSE
, such as in the example above, happens often. To make this
easier to write and read, the @ELSEIF
action is a shortcut for a @ELSE
directly followed by a @IF
.
It takes a condition, just like @IF
.
Here is the same example rewritten with @ELSEIF
:
<# @IF(age > 30) #>
Older
<# @ELSEIF(age > 20) #>
In the middle
<# @ELSE #>
Younger
<# @ENDIF #>
The way this works is that the first condition to be true is the one whose content will be displayed. If none of
the conditions are true, then the @ELSE
’s content (if there is a @ELSE
), will be displayed.
There can be multiple @ELSEIF
:
<# @IF(age > 30) #>
Older
<# @ELSEIF(age > 20) #>
In the middle
<# @ELSEIF(age > 10) #>
Younger
<# @ELSE #>
Too young
<# @ENDIF #>
Order of the actions
The actions must always be written in this order:
- Start with
@IF
- All the
@ELSEIF
(if any) go after the@IF
. @ELSE
(which is optional) goes after all@IF
and@ELSEIF
.@ENDIF
goes last.
Other things to read
There is also a tutorial covering conditional content which speaks about all this differently. This may be worth a read: Conditional content