This article mainly introduces the variable output, custom functions and judgment statement usage of ThinkPHP templates. It is a very practical skill in ThinkPHP template operations. Friends who need it can refer to it
This article explains the examples ThinkPHP template variable output, custom functions and judgment statement usage. It mainly includes three usages: variable output, custom function and judgment statement. Share it with everyone for your reference. The specific analysis is as follows:
Template operation variable output:
Quick output variable
Copy code The code is as follows:
1 2 3 4 5 6 7 8 9 |
|
Template output
Copy code The code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Use judgment statements
We can use if tags to define complex conditional judgments, for example:
Copy code The code is as follows:
1 2 3 4 |
|
The condition attribute can support eq and other judgment expressions, the same as the comparison tag above, but does not support "> The usage of symbols such as ", "<" will confuse template parsing, so the following usage is wrong:
1 2 3 |
|
must be changed to:
Copy code The code is as follows:
1 2 3 |
|
In addition, we can use php code in the condition attribute, for example:
Copy code The code is as follows:
1 2 3 |
|
The condition attribute can support dot syntax and object syntax, for example:
Automatically determine whether the user variable is an array or an object
Copy code The code is as follows:
1 2 3 |
|
Or know that the user variable is an object
Copy code The code is as follows:
1 2 3 |
|
Since the condition attribute of the if tag basically uses PHP syntax, so It may be more concise to use judgment tags and switch tags. In principle, if it can be solved with switch and comparison tags, try not to use if tags. Because switch and comparison tags can use variable modifiers and system variables. If the IF tag still cannot meet certain special requirements, you can use native PHP code or PHP tags to write code directly.
eq is equal to (==)
neq is not equal to (!=)
gt is greater than (>)
egt is greater than or equal to (>=)
lt is less than (< )
elt is less than or equal to (<=)
heq is equal to (===)
nheq is not equal to (!==)
condition condition
Note
condition attribute value , variables require the $ sign, which is different from other labels.
How to use custom functions
Function calling format of template variables: {$varname|function1|function2=arg1, arg2,
}
Usage example:
Copy code
The code is as follows:1 2 3 |
|
Copy code
The code is as follows:
1 2 3 4 5 |
|
Copy code
The code is as follows:1 |
|
Note: There can be no spaces between
{ and $ symbols, and there will be no problem with spaces in subsequent parameters;
indicates the parameter position of the template variable itself;
supports multiple functions , supports spaces between functions;
supports variable caching function, and repeated variable strings will not be parsed multiple times.
ThinkPHP template replacement and system constants and application examples######
The above is the detailed content of ThinkPHP template variable output, custom functions and judgment statement usage. For more information, please follow other related articles on the PHP Chinese website!