Building Conditional Blocks with the %switch% Tag
You can use the %switch% tag to construct a conditional expression based on the value of a specified variable. The %switch% tag allows you to define a separate block of code (a case) for each value that a variable can take at run time. You use this tag instead of %ifvar% when you have more than two possible paths of execution. (You can also handle multiple paths of execution by building multiple %ifvar% expressions, however, the %switch% tag is much easier to use and maintain for this purpose.)
The basic format of the %switch% tag is as follows, where variableName specifies the name of the variable you want to evaluate at run time and switchValue is the value that will cause a case to execute:
%switch variableName%
%case ‘switchValue’%
Block of Code
%case ‘switchValue’%
Block of Code
.
.
.
[%case%
Default Block
of Code]
%end%
At run time, the DSP Processor evaluates each %case% block in order, and executes the first block whose switchValue matches the value in variableName. After processing the code within that block, the DSP Processor skips the remaining cases in the %switch% construct.
The following example shows an %switch% construct that executes one of two cases, depending on the contents of a variable named action.
.
.
.
%swtich action%
%case ‘shipinfo’%
%invoke orders:getShipInfo%
.
.
.
%endinvoke%
%case ‘vieworder’%
%invoke orders:getOrderInfo%
.
.
.
%endinvoke%
%endswitch%