ThinkPHP template variable output, user-defined functions, and judgment statement usage _ PHP

Source: Internet
Author: User
This article mainly introduces the ThinkPHP template's variable output, user-defined functions, and judgment statement usage. it is a very useful technique for ThinkPHP template operations. For more information, see ThinkPHP.

This article describes the ThinkPHP template's variable output, user-defined functions, and judgment statement usage. It mainly includes variable output, user-defined functions, and judgment statements. Share it with you for your reference. The specific analysis is as follows:

Template operation variable output:

Quick output variable

The code is as follows:

{: Function (...)} // Execute the method and output the returned value
{~ Function} // the execution method is not output
{@ Var} // output the Session variable
{# Var} // output Cookie variable
{& Var} // output configuration parameters
{% Var} // output language variable
{. Var} // output GET variable
{^ Var} // output the POST variable
{* Var} // output constant


Template output

The code is as follows:

// 1 directly call the index operation Template
$ This-> display ();
// Corresponding to Myapp/Tpl/default/Index/index.html
// 2 call the test1 operation template of the Index module
$ This-> display ('test1 ');
// Corresponding to Myapp/Tpl/default/Index/test1.html
// 2 call the test2 operation template of the Message module ()
$ This-> display ('Message: test2 ');
// Corresponding to Myapp/Tpl/default/Message/test2.html
// 3 call the test2 operation template of the Message module of the XP topic
$ This-> display ('XP @ Message: test2 ');
// Corresponding to Myapp/Tpl/Xp/Message/test2.html
// 4 specify the full name of the template file.
$ This-> display ('../Message/test3.html ');
// Corresponding to Myapp/Tpl/default/Message/test3.html

Use judgment statement

We can use the if tag to define complex condition judgments, for example:

The code is as follows:

Value1
Value2
Value3

In the condition attribute, you can support eq and other judgment expressions. comparison labels are the same as above, but they do not support the use of symbols such as ">" and "<", because template parsing is obfuscated, so the following usage is incorrect:

Value1
Value2


Must be changed:

The code is as follows:

Value1
Value2


In addition, we can use php code in the condition attribute, for example:

The code is as follows:

ThinkPHP
Other Framework


The condition attribute supports dot syntax and object syntax, for example:
Automatically determines whether the user variable is an array or an object

The code is as follows:

ThinkPHP
Other Framework


Or you know that the user variable is an object.

The code is as follows:

ThinkPHP
Other Framework

Because the condition attribute of the if tag basically uses the php syntax, it is more concise to use the judgment tag and Switch tag. in principle, if you can use the switch or comparison label, try not to use the if label. Because switch and comparison labels can use variable regulators and system variables. IF the IF tag still cannot meet the requirements under some special requirements, you can use native php code or PHP label to directly write the code.

Eq equals (=)
Neq is not equal (! =)
Gt greater than (>)
Egt greater than or equal to (> =)
Lt is less than (<)
Elt less than or equal to (<=)
Heq constant equals (=)
Nheq is not always equal (! =)
Condition

Note:
In the condition property value, the variable requires the $ symbol, which is different from other labels.

How to use UDFs

Template variable function call format: {$ varname | function1 | function2 = arg1, arg2 ,###}

Example:

The code is as follows:

{$ WebTitle | md5 | strtoupper | substr = 0, 3}
{$ Number | number_format = 2}
{$ Varname | function1 | function2 = arg1, arg2 ,###}

Example:

The code is as follows:

Function Cate ($ cid ){
$ Cate = D ('Cate ');
$ Cate = $ Cate-> where ('Id = '. $ cid)-> find ();
Return $ Cate ['title'];
}


I want to call this function in the template, so you can write it like this in the template.

The code is as follows:

{$ Vo. cid | cate ###}


Note: User-defined functions should be stored in the project application directory/common. php. Here is the key.

Note:
{There must be no space between the symbol and the $ symbol, so there is no problem with the space of the following parameter;
### Parameter location of the template variable itself;
Multiple functions are supported, and spaces are supported between functions;
Function shielding is supported. in the configuration file, you can configure a list of disabled functions;
Variable caching is supported. repeated variable strings are parsed several times.

I hope this article will help you with ThinkPHP framework 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.