switch

EVML switch statement evaluates an expression, based on case clauses, and executes statements associated with that case. In EVML a switch statement must be contained within a single <?ev..?> script code block. It may not be broken to render HTML. 

Syntax

<?ev
    switch(expression){
        case value1:
            // Statements executed if expression matches value1
            [break;]
        case value2:
            // Statements executed if expression matches value2
            [break;]
        ...
        case valueN:
            // Statements executed if expression matches valueN
            [break;]
        default:
            // Statements executed when none of the values match expression
            [break;]
    }
?>

Where...

expression
An expression tested against each case clause.
case value
The value to test against the expression.
default
The statements to execute if no case clauses are met.


How switch statements work

A switch statement evaluates its expression, then looks for the first matching case clause. It will then continue to match case clauses unless a break statement is encountered. If no matching case clauses are found the default clause will execute its associated statements (if available). The default clause is generally added as the last clause by convention.

Example: common usage

In the example below we examine the condition of a product to deliver a more friendly message to the user.

<?ev
    switch(product.condition){
        case 'used':
            print('This is a used product, but in great condition');
            break;
        case 'refurbished':
            print("We've refurbished this product to guarantee its quality");
            break;
        case 'new':
        default:
            print('This is a new product, unopened and pristine');
            break;
    }
?>