ThinkPHP User-Defined Function solves the method of adding and subtracting template labels. thinkphp Template

Source: Internet
Author: User

ThinkPHP User-Defined Function solves the method of adding and subtracting template labels. thinkphp Template

This example describes how ThinkPHP UDF solves the addition and subtraction of template labels. Share it with you for your reference. The details are as follows:

In actual projects, we often need to add or subtract tag variables. However, in ThinkPHP, direct operations on Template variables are not supported.

Fortunately, it provides methods for user-defined functions. We can solve this problem using user-defined functions:

The syntax of the ThinkPHP template custom function is as follows:

Format: {: function (...)} (Refer to official help documentation: http://thinkphp.cn/Manual/196)
Use this to try addition and subtraction.

1. define functions in ThinkPHP. Create a common. php file in the common folder of the project (the system will automatically load the file ). Define two functions:

/*** Is added for the template * @ param <type> $ a * @ param <type> $ B */function template_add ($ a, $ B) {echo (intval ($ a) + intval ($ B);}/*** subtraction, for template use * @ param <type> $ a * @ param <type> $ B */function template_substract ($ a, $ B) {echo (intval ($) -intval ($ B ));}

2. Use functions in the template:
Copy codeCode: {: template_add ($ var1, $ var2 )}
The sum of the variable var1 and var2.
Note that if the variable is an array, it should be displayed as follows:
Copy codeCode: {: template_add ($ var [var1], $ var [var2])}
Instead, we usually use the dot syntax.

I hope this article will help you with ThinkPHP-based php programming.

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: info-contact@alibabacloud.com 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.