Only the output variables cannot meet the requirements of template output. The built-in template engine supports the use of the regulator and formatting functions for template variables. In fact, it provides Function Support and supports simultaneous use of multiple functions. The function used for the template tag can be a PHP built-in function or a user-defined function. Unlike smarty, the function used for the template does not need to be defined.
The function call format of template variables is:
XML/HTML code
{$ Varname | function1 | function2 = arg1, arg2 ,###}
Note:
{There cannot be spaces between the symbol and the $ symbol, so there is no problem with the space of the following parameter.
### Parameter location of template Variables
Supports multiple functions and spaces between functions
Function blocking is supported. You can configure a list of functions that are not allowed in the configuration file.
Variable caching is supported. Repeated variable strings are parsed several times.
Example:
XML/HTML code
{$ WebTitle | md5 | strtoupper | substr = 0, 3}
The compiled PHP code is:
PHP code
<? Php echo (substr (strtoupper (md5 ($ webTitle), 0, 3);?>
Pay attention to the correspondence between the function definition and the sequence of use. Generally, the first parameter of a function is the result of the preceding variable or the previous function, if your variable is not the first parameter of the function, you need to use the positioning symbol, for example:
XML/HTML code
{$ Create_time | date = "y-m-d ",###}
The compiled PHP is:
PHP code
<? Php echo (date ("y-m-d", $ create_time);?>
There is no limit on the number of functions used. However, you can configure TMPL_DENY_FUNC_LIST to disable the function list. The exit and echo functions are disabled by default to prevent damage to the template output. We can also add additional definitions, for example:
TMPL_DENY_FUNC_LIST => "echo, exit, halt"
Use commas to separate multiple functions.
In addition, quick methods for directly calling functions in the template file are provided, without using template variables. There are two methods:
1. Execute the method and output the returned value:
Format: {: function (...)}
For example, the return value of the output U method:
XML/HTML code
{: U ('user/insert ')}
The compiled PHP code is
PHP code
<? Php echo U ('user/insert');?>
2. Execution method but no output:
Format :{~ Function (...)}
For example, call the say_hello function:
XML/HTML code
{~ Say_hello ('thinkphp ')}
The compiled PHP code is:
PHP code
<? Php say_hello ('thinkphp');?>
For example, my custom function is like this.
Function Category ($ cid)
{
$ Category = D ("Category ");
$ Category = $ Category-> where ('Id = '. $ cid)-> find ();
Return $ Category ['title'];
}
I want to call this function in the template, so you can write it like this in the template.
{$ Vo. cid | category ###}
Of course, the user-defined functions should be stored in the project directory/Common/common. php.
In actual projects, we often need to add or subtract tag variables. However, in ThinkPHP, direct operations on Template variables are not supported.
Fortunately, it provides methods for user-defined functions. We can solve this problem using user-defined functions:
The syntax of the ThinkPHP template custom function is as follows:
Format: {: function (...)} (Refer to official help documentation: http://thinkphp.cn/Manual/196)
Use this to try addition and subtraction.
Define a function in ThinkPHP. Create a common. php file in the common folder of the project (the system will automatically load the file ). Define two functions:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
Function
Template_add ($ a, $ B ){
Echo (intval ($ a) + intval ($ B ));
}
Function
Template_substract ($ a, $ B ){
Echo (intval ($ a)-intval ($ B ));
}
Use functions in the template:
{: Template_add ($ var1, $ var2 )}
The sum of the variable var1 and var2.
Put the User-Defined Function in the Common. php Directory. If the call fails, change the file name to common. class. php and define import (@. common. common)