<b:switch> tag, is a type of conditional statement that will evaluate an expression against multiple possible cases and execute one or more blocks of code based on matching cases. It includes <b:case/> and <b:default/> statement.

The Switches Blogger Syntax

Blogger XML Language
<b:switch var='EXPRESSION'>
    <b:case value='EXPRESSION1'/>
        <!-- execute case match VALUE1 code block -->
    <b:case value='EXPRESSION2'/>
        <!-- execute case match  VALUE2 code block -->
    <b:default/>
        <!-- execute case default code block if all case doesn't meet. -->
</b:switch>
  • Tags <b:switch> can be nest.
  • Tags <b:switch> and <b:case> must have attribute.
  • Both <b:switch> and <b:case> value is either Boolean, Number, or String or 'data:' blogger expression.
  • The tags <b:case> and <b:default> must be nested in a tag <b:switch>.
  • The <b:case> tags are required and the tag <b:default> is not necessary.
  • The tag <b:case> can be used multiple times in a tag <b:switch>.
  • The tag <b:default> can only be used once in a tag <b:switch>.  
  • The tag <b:default> should be the last tag to be mentioned in the switch.

Switches tag Example

With String
<ul>
    <b:loop values='["Fruit","Food","Other"]' var='MyList'>
        <li><data:MyList/></li>
        <b:switch var='data:MyList'>
            <b:case value='Fruit'/>
                <b:loop values='["Apple","Mango","Orange"]' var='Fruit'>
                    <ul>
                        <li><data:Fruit/></li>                  
                    <b:switch var='data:Fruit'>
                        <b:case value='Apple'/>
                            <b:loop values='["Gala","Fuji"]' var='Kind'>
                            <ul>
                                <li><data:Kind/></li>
                            </ul>
                            </b:loop>
                    </b:switch>
                    </ul>
                </b:loop>
            <b:case value='Food'/>
            <b:loop values='["Meat","Vegetable"]' var='Food'>
                <ul>
                    <li><data:Food/></li>
                </ul>
            </b:loop>
            <b:default/>
            <b:loop values='["Milk","Seafood","Juice","Banana"]' var='Unlisted'>
                <ul>
                    <li><data:Unlisted/></li>
                </ul>
            </b:loop>
        </b:switch>
    </b:loop>
</ul>
HTML Rendered Result
  • Fruit
    • Apple
      • Gala
      • Fuji
    • Mango
    • Orange
  • Food
    • Meat
    • Vegetable
  • Other
    • Milk
    • Seafood
    • Juice
    • Banana

In the example, we have three data in the loop, its ready to print the output. But, before that, he found a Switch and have different case for every data we have earlier. The case is processing main data and adds another data to the main. The process repeat until all Switch and Case fulfilled, so, now our data will look like rendered result above.

Other Tag

Share This

About
BloggerBasics101 is licensed under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
Misc
Course