Template operation variable output
Quick output variable
The code is as follows: |
Copy code |
{: Function (...)} // Execute the method and output the returned value {~ Function} // the execution method is not output {@ Var} // output the Session variable {# Var} // output Cookie variable {& Var} // output configuration parameters {% Var} // output language variable {. Var} // output GET variable {^ Var} // output the POST variable {* Var} // output constant Template output // 1 directly call the index operation Template $ This-> display (); // Corresponding to Myapp/Tpl/default/Index/index.html // 2 call the test1 operation template of the Index module $ This-> display ('test1 '); // Corresponding to Myapp/Tpl/default/Index/test1.html // 2 call the test2 operation template of the Message module () $ This-> display ('Message: test2 '); // Corresponding to Myapp/Tpl/default/Message/test2.html // 3 call the test2 operation template of the Message module of the XP topic $ This-> display ('XP @ Message: test2 '); // Corresponding to Myapp/Tpl/Xp/Message/test2.html // 4 specify the full name of the template file. $ This-> display ('../Message/test3.html '); // Corresponding to Myapp/Tpl/default/Message/test3.html |
Use judgment statement
We can use the if tag to define complex condition judgments, for example:
The code is as follows: |
Copy code |
<If condition = "($ name eq 1) OR ($ name gt 100)"> value1 <Elseif condition = "$ name eq 2"/> value2 <Else/> value3 </If>
|
In the condition attribute, you can support eq and other judgment expressions. Comparison labels are the same as above, but they do not support the use of symbols such as ">" and "<", because template parsing is obfuscated, so the following usage is incorrect:
The code is as follows: |
Copy code |
<If condition = "$ id <5"> value1 <Else/> value2 </If> Must be changed: <If condition = "$ id lt 5"> value1 <Else/> value2 </If>
|
In addition, we can use php code in the condition attribute, for example:
The code is as follows: |
Copy code |
<If condition = "strtoupper ($ user ['name']) neq 'thinkphp'"> THINKPHP <Else/> other Framework </If>
|
The condition attribute supports dot syntax and object syntax, for example:
Automatically determines whether the user variable is an array or an object
The code is as follows: |
Copy code |
<If condition = "$ user. name neq 'thinkphp'"> ThinkPHP <Else/> other Framework </If>
|
Or you know that the user variable is an object.
The code is as follows: |
Copy code |
<If condition = "$ user: name neq 'thinkphp'"> ThinkPHP <Else/> other Framework </If>
|
Because the condition attribute of the if tag basically uses the php syntax, it is more concise to use the judgment tag and Switch tag. In principle, if you can use the switch or comparison label, try not to use the if label. Because switch and comparison labels can use variable regulators and system variables. IF the IF tag still cannot meet the requirements under some special requirements, you can use native php code or PHP label to directly write the code.
Eq equals (=)
Neq is not equal (! =)
Gt greater than (>)
Egt greater than or equal to (> =)
Lt is less than (<)
Elt less than or equal to (<=)
Heq constant equals (=)
Nheq is not always equal (! =)
Condition
Note:
In the condition property value, the variable requires the $ symbol, which is different from other labels.
How to use UDFs
Template variable function call format: {$ varname | function1 | function2 = arg1, arg2 ,###}
The code is as follows: |
Copy code |
Example: {$ WebTitle | md5 | strtoupper | substr = 0, 3} {$ Number | number_format = 2} {$ Varname | function1 | function2 = arg1, arg2 ,###} Instance:
Function Cate ($ cid ){ $ Cate = D ('Cate '); $ Cate = $ Cate-> where ('Id = '. $ cid)-> find (); Return $ Cate ['title']; }
|
I want to call this function in the template, so you can write it like this in the template.
{$ Vo. cid | cate ###}
Note: User-defined functions should be stored in the project application directory/common. php. Here is the key.
Note:
{There must be no space between the symbol and the $ symbol, so there is no problem with the space of the following parameter;
### Parameter location of the template variable itself;
Multiple functions are supported, and spaces are supported between functions;
Function shielding is supported. In the configuration file, you can configure a list of disabled functions;
Variable caching is supported. Repeated variable strings are parsed several times.
Now, we are ready to go. We will share more study notes on thinkphp templates later.