Template Syntax
Switch-Case



A switch-case structure has the same form as in Java:

<switch expression>
    <case value1>
        HTML for case 1.
        <break>
    <case value2>
        HTML for case 2.
        <break>
    <default>
        HTML for the default case.
        <break>
</switch>

You can have as many cases as you like. The values value1, value2, etc., must be valid expressions. The <default> case is optional. The rules for what gets executed are the same as in Java; if you don't end a case with <break>, it will fall through to the next case. For example:

<switch animal.name>
    <case "elephant">
    <case "rhinocerous">
        This is the HTML for large animals.
        <break>
    <case "squirrel">
    <case "gecko">
        This is the HTML for small animals.
        <break>
    <case favoriteAnimal.name>
        This is the HTML for the user's favorite animal.
        <break>
    <default>
        This is the HTML for other animals.
        <break>
</switch>

Previous: If-Else Next: Assignment