Just the output variable does not meet the needs of the template output, the built-in template engine supports the use of the template variable regulator and formatting functions, in fact, provide function support, and support the use of multiple functions simultaneously. Functions used for template labels can be PHP built-in functions or user-defined functions, unlike smarty, which do not require special definitions for the functions used in templates.
The function call format for template variables is:
xml/html Code
{$varname |function1|function2=arg1,arg2,###}
Description
There can be no spaces between {and $ symbols, there is no problem with the space behind the argument
# # #表示模板变量本身的参数位置
Support for multiple functions, space between functions
Supports function masking, which allows you to configure a list of prohibited functions in a configuration file
Support variable cache function, repeat variable string not multiple parsing
Examples of Use:
xml/html Code
{$webTitle |md5|strtoupper|substr=0,3}
The compiled PHP code is:
PHP code
Note that the definition of the function and the corresponding relationship of the order of use, usually the first parameter of the function is the previous variable or the result of the previous function, if your variable is not the first parameter of the function, you need to use a positional symbol, for example:
xml/html Code
{$create _time|date= "y-m-d", # # #}
The compiled PHP is:
PHP code
There is no limit to the use of functions, but you can allow configuration tmpl_deny_func_list to define a list of disabled functions, and the Exit and ECHO functions are disabled by default to prevent damage to the template output, and we can add additional definitions, such as:
Tmpl_deny_func_list=> "Echo,exit,halt"
Multiple functions are separated by commas.
It also provides a quick way to call a function directly in a template file, without having to pass through a template variable, including two different ways:
1. Execute the method and output the return 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
2, the execution method but does not output:
Format: {~function (...)}
For example, call the Say_hello function:
xml/html Code
{~say_hello (' thinkphp ')}
The compiled PHP code is:
PHP code
Like my custom function.
function Category ($CID)
{
$Category =d ("Category");
$Category = $Category->where (' id= '. $cid)->find ();
return $Category [' title '];
}
I want to call this function in the template, which I can write in the template.
{$vo. cid|category=###}
Of course, the custom function should be placed in the project directory/common/common.php
In the actual project, we often need the operation of the tag variable plus minus operation. However, in thinkphp, the operation of the direct operation of the template variable is not supported.
Fortunately, it provides a way to customize the function, and we can use the custom function to solve:
The thinkphp template Custom function syntax is as follows:
Format: {: function (...)} (refer to official Help document: http://thinkphp.cn/Manual/196)
Using this, let's try adding and subtracting.
Define the function in thinkphp. Create a new common.php file under the project's Common folder (so the system will load automatically). 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));
}
To use a function in a template:
{: Template_add ($var 1, $var 2)}
You can show the var1 of variables in var2.
Put the custom function in the common directory common.php if the call does not come out, change the file name to common.class.php, define import (@.common.common)
http://www.bkjia.com/PHPjc/477992.html www.bkjia.com true http://www.bkjia.com/PHPjc/477992.html techarticle just the output variable does not meet the needs of the template output, the built-in template engine supports the use of the template variable regulator and formatting functions, in fact, provide function support, and support more ...