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

thinkphp frontend html call function formatted output_PHP tutorial

Jul 13, 2016 pm 05:53 PM
html thinkphp built-in function variable engine format template satisfy of transfer output need

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

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

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

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

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

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.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

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

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

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

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

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

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

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

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

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

See all articles