If constructs provide a means to execute portions of the macro based on conditions that you specify. An if construct consists of the following components:
The if expression and any optional elseif expressions must include a lone environment value command, a local variable, a literal, or some operation using one or more operators.
Only one of the groups of expressions within the if construct is executed, according to the following scheme:
If all elseif expressions evaluate to false (zero) or if no elseif expressions are present, then the else expression group—if present—is executed.
You can write an empty expression group so that no action is performed if this group is selected for execution. You can nest if structures within other if structures or while structures.
The following sample macro demonstrates various if structures:
<# if_examples #> <# //---------------------------------------- #>
<# if 1 #> ! This is always output because any nonzero value is “ true.” <# endif #>
<# if 0 #> ! This is never output because a value of zero is “ false.” <# endif #>
<# // Here’s an example with elseif and else. #>
<# color := env.getline("What is your favorite color? ") #>
<# if color = "red" #>
! Red is my favorite color, too.
<# elseif color = "pink" #>
! Pink is a lot like red.
<# elseif color = "black" #>
! Black is just a very, very, very dark shade of red.
<# else #>
! Oh. That’s nice.
<# endif #>
<# // Here’s a nested if example. #>
<# sure := env.getline("Are you sure that " $ color $ " is your favorite color? ") #>
<# if substr(sure, 0, 1) = ’y’ || substr(sure, 0, 1) = ’Y’ #>
<# if color != "black" && color != "white";
shade := env.getline("Do you prefer dark " $ color $
" or light " $ color $ "? ") #>
<# if shade = "dark" #>
! I like dark colors, too.
<# elseif shade = "light" #>
! I prefer dark colors myself.
<# else #>
! Hmmm, that’s neither dark nor light.
<# endif #>
<# else #>
! Oh. That’s nice.
<# endif #>
<# else #>
! I didn’t think so!
<# endif #>
<# endtmpl #>