Home > Backend Development > PHP Tutorial > thinkphp frontend html call function formatted output_PHP tutorial

thinkphp frontend html call function formatted output_PHP tutorial

WBOY
Release: 2016-07-13 17:53:55
Original
963 people have browsed it

Merely outputting variables cannot meet the needs of template output. The built-in template engine supports the use of regulators and formatting functions for template variables. In fact, it provides function support and supports the use of multiple functions at the same time. The functions used for template tags can be PHP built-in functions or user-defined functions. Unlike smarty, the functions used for templates do not need to be specially defined.

The function calling format of template variables is:


XML/HTML code


{$varname|function1|function2=arg1,arg2,### }


Description:

There can be no space between

{ and the $ symbol, and there will be no problem with spaces in the following parameters

### represents the parameter position of the template variable itself

Supports multiple functions and spaces between functions

Supports function blocking function, and the list of prohibited functions can be configured in the configuration file

Supports variable caching function, repeated variable strings will not be parsed multiple times

Usage example:


XML/HTML code


{$webTitle|md5|strtoupper|substr=0,3}


The compiled PHP code is:


PHP code



Pay attention to the correspondence between the definition of the function and the order of use. Generally speaking, 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 positioning symbols. 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, but you can configure TMPL_DENY_FUNC_LIST to define a list of disabled functions. The system disables exit and echo functions by default to prevent damage to template output. We can also add additional definitions, such as:

TMPL_DENY_FUNC_LIST=>"echo,exit,halt"

Use half-width commas to separate multiple functions.

And also provides a shortcut method to call functions directly in template files without passing template variables, including two methods:

1. Execute the method and output the return value:

Format: {:function(…)}

For example, output the return value of U method:


XML/HTML code


{:U('User/insert')}

The compiled PHP code is


PHP code



2. Execute the method without 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'];

}
If I want to call this function in the template, I can write this in the template
{$vo.cid|category=###}
Of course, the custom function must be placed in the project directory/Common/common.php


In actual projects, we often need to perform addition and subtraction operations on label variables. However, in ThinkPHP, direct operation of template variables is not supported.
Fortunately, it provides a method of custom functions, which we can solve using custom functions:
The syntax of ThinkPHP template custom function is as follows:
Format: {:function(…)} (Refer to the official help document: http://thinkphp.cn/Manual/196)
Using this, let's try addition and subtraction.

Define functions in ThinkPHP. Create a new common.php file in the common folder of the project (the system will load it 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));

}
Use functions in templates:
{:template_add($var1,$var2)}
The sum of variables var1 and var2 can be displayed.

Put the custom function in common.php in the Common directory. If it cannot be called, change the file name to common.class.php and define import(@.common.common)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477992.htmlTechArticleJust outputting variables does not meet the needs of template output. The built-in template engine supports the use of regulators and formats for template variables. The function is actually to provide function support and support multiple...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template