


thinkphp frontend html call function formatted output_PHP tutorial
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:
{ 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)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
