thinkphp Foreground HTML call function formatted output _php tutorial

Source: Internet
Author: User
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,###}


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:
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:





















Template_add ($a, $b) {

Echo (Intval ($a) +intval ($b));



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) true 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 ...

  • Related Article

    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: 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.