Thinkphp foreground html call function formatting output

Source: Internet
Author: User
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 functions used for template tags can be PHP built-in functions or user-defined functions, and... SyntaxHighlighter. all ();

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


 

 


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


 


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


 

 


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


 

 
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.