ThinkPHP Template Switch Tag Usage Example

  • 2021-07-06 10:29:53
  • OfStack

The switch tag in the ThinkPHP template is used to output different values in the template according to different situations.

ThinkPHP template engine supports switch judgment, and can output different results according to different conditions. The format is as follows:


<switch name=" Variable name " >
<case value=" Value 1"> Output content 1</case>
<case value=" Value 2"> Output content 2</case>
<default /> Default 
</switch>

Examples of usage are as follows:


<switch name="uid">
<case value="1"> Administrator </case>
<default /> Tourists 
</switch>

Note here:

name attributes can use functions as well as system variables, and their attribute values are variable names without the $sign, whereas value values use variables with the $sign, as shown in the above example.

However, when the value of value uses variable mode, it no longer supports simultaneous judgment of multiple conditions, such as the following multi-condition judgment.

Multi-condition judgment:
The value attribute of case can support simultaneous judgment of multiple conditions and use symbols for segmentation:


<switch name="Think.get.type">
<case value="gif|png|jpeg"> Image format file </case>
<default /> Other format files 
</switch>

This means that if the value of $_ GET ["type"] is gif, png or jpg, the image format is determined.

name properties can also be system variables and can use functions such as:


<switch name="Think.get.username|function1">
......
</switch>

Related articles: