Https://www.cnblogs.com/jingmin/p/6440673.html
1. Using PHP Functions in templates
in the thinkphp HTML, we often encounter some variables are difficult to directly from the PHP control side directly processing, these variables only in the template loop output when processing more appropriate, this time, we will use the function in the template
1.1 Using PHP functions for output templates
{$data. NAME|MD5}//MD5 encryption of the name variable in the template
Translate this sentence into the PHP language:
Echo (MD5 ($data [' name ']);?>
1.2 Multiple arguments in the function need to be called
1.2.1 The previously output variable when the second argument of the function is used
{$create _time|date= "y-m-d", # # #}
Explanation: The date function passed two parameters, each parameter is separated by commas, here the first argument is, the second argument is the y-m-d
variable to be output before, create_time
because the variable is the second parameter, so you need to use # # #标识变量位置.
Translate into PHP language:
Echo (date ("y-m-d",$create _time))?>
1.2.2 The previously output variable when the first argument of the function is used
{$data. name|substr=0,3}
Or
{$data. name|substr=###,0,3}
Translate into PHP language
Echo (substr ($data [' name '],0,3));?>
1.3 Handling of multiple functions for a variable
{$name |md5|strtoupper|substr=0,3}
Each function is separated by an 丨 symbol, and the function execution order is called from left to right
Or
{:substr (strtoupper (MD5 ($name)), 0,3)}
Compile into PHP language:
Echo (substr (strtoupper (MD5 ($name)), 0,3));?>
2. Variables output in templates use custom functions
In the project, in addition to some PHP functions, we can also according to the actual needs of our project, in the project application directory/common/function.php, write their own custom functions
Key Note: {and $ sign can not have a space, the following parameters of the space is no problem;
# # #表示模板变量本身的参数位置;
Supports multiple functions and supports spaces between functions;
Support function masking function, in the configuration file can be configured to prohibit the use of the list of functions;
The variable cache function is supported, and the repeating variable string is not resolved multiple times.
2.1 Use of custom functions
Simple custom functions are basically consistent with PHP functions.
2.2 Advanced use of custom functions
{$varname |function1|function2=arg1,arg2,# ##}
Translate into PHP code:
Echo (function2 (Function1 (arg1,arg2,$varname));?>
2.3 Cases
I write a method in function.php
function Cate ($cid$Cate =d (' Cate '$Cate =$Cate->where (' id= '). $CID)$Cate [' title '];}
Call this custom function in the template:
{$vo. cid|cate=# ##}
I'm just, for example, the general method of writing in a function is a more public-use method in the project.
Use of functions in thinkphp templates