The mgiSwitch Tag

Tag Behavior

Use mgiSwitch tag to perform conditional comparisons to known cases. The syntax for the mgiSwitch tag is similar to that of a c-style switch statement. (see also mgiIf and mgiInlineIf)


Tag Syntax

The mgiSwitch tag has a beginning tag with one required parameter and one optional parameter, a body, and an ending tag. The mgiCase tag is used within the mgiSwitch tag to define comparison cases. The mgiCase tag has a beginning tag with no required parameters and one optional parameter, a body , and an ending tag (The mgiCase tag may have multiple beginning tags for any single ending tag). The tag forms are:

<mgiSwitch value="Value" caseSensitive="Yes/No">
  <mgiCase value="Comparison Value">
  Value if comparison to case(s) is true
  </mgiCase>
  <mgiCase>
  Default value if no comparison is true or 
  if no comparison exists
  </mgiCase>
</mgiSwitch>

mgiSwitch

Required Parameters:

  • value - The value in mgiSwitch is the string tested in the conditional comparison with the mgiCase values. In order to use special reserved characters such as tabs, backslashes, and ASCII characters, you must use the appropriate Escaped String format. The case value is not case-sensitive unless you specify a specific case using the Escaped String format or include the caseSensitive parameter.

Optional Parameters:

  • caseSensitive - The caseSensitive parameter specifies whether the switch values and case values are checked for case-sensitivity. If the caseSensitive parameter value is "Yes", the switch and case values are checked for case-sensitivity. If the caseSensitive parameter values is "No", the switch and case values are not checked for case-sensitivity. The default value is "No".

mgiCase

Required Parameters:

  • None.

Optional Parameters:

  • value - The value in mgiCase is the string compared to the value in the mgiSwitch tag. The body of the first mgiCase value that is matched is displayed. Cases are tested until an ending mgiCase tag is found. If the value parameter is not included, then the case is considered to be the default result. The value parameter of the mgiCase tag is not preprocessed by MGI (i.e., MGI tags are not processed within the value parameter). In order to use special reserved characters such as tabs, backslashes, and ASCII characters, you must use the appropriate Escaped String format. The case value is not case-sensitive unless you specify a specific case using the Escaped String format or include the caseSensitive parameter in the mgiSwitch tag. All case values are constant, therefore you cannot dynamically populate case values by embedding MGI tags.


Example Usage and Output

The mgiSwitch equivalent to the c-style switch statement: C-style switch statement:
<mgiSwitch value="checkvalue">
    <mgiCase value="3">
        3
    <mgiCase value="2">
        2
    <mgiCase value="1">
        1
    </mgiCase>
    <mgiCase>
        default
    </mgiCase>
</mgiSwitch>
c++ (pseudo-code):

    switch(checkvalue)
    {
        case "3":
            3;
        case "2":
            2;
        case "1":
            1;
            break;
        default:
            "default";
            break;
    }

Your letter grade is:
<mgiSwitch value={mgiGet name="Score"}>
    
    <mgiCase value="100">
    <mgiCase value="95">
    <mgiCase value="90">
        A
    </mgiCase>

    <mgiCase value="85">
    <mgiCase value="80">
        B
    </mgiCase>

    <mgiCase value="75">
    <mgiCase value="70">
        C
    </mgiCase>

    <mgiCase value="65">
        D
    </mgiCase>

    <mgiCase>
        F
    </mgiCase>

</mgiSwitch>

In this example, a calculated quiz score is compared to possible quiz scores in order to determine the student's letter grade. If the student's quiz score is "95", then their letter grade will appear as:

Your letter grade is: A

<mgiSwitch value={mgiRequest name="Username"}>
  <mgiCase value="Member">
    <mgiIncludeFile name="member.mgi">
  </mgiCase>
  <mgiCase value="Admin">
    <mgiIncludeFile name="admin.mgi">
  </mgiCase>
  <mgiCase>
    <mgiIncludeFile name="index.mgi">
  </mgiCase>

</mgiSwitch>

In this example, the content of a page is dynamically displayed based on a user's login information. If a user enters "Member" for their login, then the member.mgi page is included and displayed. If a user enters "Admin" for their login, then the admin.mgi page is included and displayed. If any other login is entered, then the index.mgi page is included and displayed.


Suggested Usage

  • Conditional Comparisons
  • Dynamic Content


[Understanding MGI Menu] [Using MGI Menu] [Referencing MGI Menu]


[MGI Guides Main Menu] [User Guide Main Menu]