


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)

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
