_php example of variable output, custom function and judgment statement usage of thinkphp template

Source: Internet
Author: User
Tags constant php template smarty template

This paper describes the variable output, the custom function and the judgment statement usage of the thinkphp template. It mainly includes three kinds of usage of variable output, custom function and judgment statement. Share to everyone for your reference. The specific analysis is as follows:

Template operation variable output:

Quick Output Variables

Copy Code code as follows:
{: function (...)} Execute method and output return value
{~function}//execution method does not output
{@var}//Output session variable
{#var}//Output cookie variable
{&var}//Output configuration parameters
{%var}//Output language variable
{. var}//Output get variable
{^var}//Output post variable
{*var}//Output constant

Stencil output
Copy Code code as follows:
1 direct call to the Index action template
$this->display ();
corresponding myapp/tpl/default/index/index.html
2 Calling the Test1 action template for the index module
$this->display (' test1 ');
corresponding myapp/tpl/default/index/test1.html
2 calling the message module's Test2 action template ()
$this->display (' Message:test2 ');
corresponding myapp/tpl/default/message/test2.html
3 The Test2 action template for the message module that invokes the XP theme
$this->display (' Xp@message:test2 ');
corresponding myapp/tpl/xp/message/test2.html
4 Specify the full name of the template file directly
$this->display ('.. /message/test3.html ');
corresponding myapp/tpl/default/message/test3.html

Use Judgment statements

We can use the IF tag to define complex conditional judgments, such as:

Copy Code code as follows:
<if condition= "($name eq 1) OR ($name gt)" > value1
<elseif condition= "$name eq 2"/>value2
<else/> Value3
</if>
In the condition attribute, you can support an EQ and other judgment expression, with the comparison label above, but do not support the use of symbols with ">", "<", because it confuses template parsing, so the following usage is incorrect:

<if condition= "$id < 5" > value1
<else/> value2
</if>


Must be changed to:
Copy Code code as follows:
<if condition= "$id lt 5" > value1
<else/> value2
</if>

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

Copy Code code as follows:
<if condition= "Strtoupper ($user [' name ']) neq ' thinkphp '" > thinkphp
<else/> Other Framework
</if>

The Condition property can support point syntax and object syntax, for example:
Automatically determine whether the user variable is an array or an object

Copy Code code as follows:
<if condition= "$user. Name neq ' thinkphp '" > thinkphp
<else/> Other Framework
</if>

Or know that the user variable is an object

Copy Code code as follows:
<if condition= "$user: Name Neq ' thinkphp '" > thinkphp
<else/> Other Framework
</if>

Because the condition property of the IF tag basically uses the PHP syntax, it is simpler to use the judge and switch tags as much as possible, in principle, to be able to use switch and comparison tags to do as much as possible without the IF tag. Because switch and comparison labels can use variable regulators and system variables. If some special requirements below, if tag still can't meet the requirements, you can use the native PHP code or PHP tag to write the code directly.

EQ equals (= =)
Neq not equal to (!=)
GT greater Than (>)
EGT is greater than or equal to (>=)
LT is less than (<)
ELT is less than equal (<=)
HEQ constant equals (= = =)
Nheq not constant equals (!==)
Condition conditions

Attention
condition attribute values, variables are required to be $ symbols, which are different from other tags.

Ways to use custom functions

function call format for template variables: {$varname |function1|function2=arg1,arg2,###}

Use examples:

Copy Code code as follows:
{$webTitle |md5|strtoupper|substr=0,3}
{$number |number_format=2}
{$varname |function1|function2=arg1,arg2,###}

Examples are as follows:

Copy Code code 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 I can write it in the template
Copy Code code as follows:
{$vo. cid|cate=###}

Note: The custom function should be placed in the project application directory/common/common.php. Here is the key.

Description
{and $ symbol can not have spaces, the trailing parameters of the space is not a problem;
# # #表示模板变量本身的参数位置;
Supports multiple functions, and supports spaces between functions;
Support function shielding function, in the configuration file can be configured to prohibit the use of the list of functions;
Variable caching is supported, and repeated variable strings are not parsed multiple times.

More interested in thinkphp related content readers can view the site topics: "thinkphp Introductory Course", "thinkphp Template Operation Skills Summary", "thinkphp Common Methods Summary", "Smarty Template Introductory Course" and "PHP template technology Summary."

I hope this article will help you with the PHP program design based on thinkphp framework.

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.