Variable Output Custom Function and Judgment Statement Usage of ThinkPHP Template

  • 2021-07-24 10:28:41
  • OfStack

This paper describes the variable output, custom function and judgment statement usage of ThinkPHP template with examples. It mainly includes three usages: variable output, custom function and judgment statement. Share it for your reference. The specific analysis is as follows:

Template operation variable output:

Quick output variable

{:function( … )} // Execute the method and output the return value 
{~function} // Execute 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

Template output
//1  Direct call index Operation template 
$this->display();
// Correspondence Myapp/Tpl/default/Index/index.html
//2 Call Index Modular test1 Operation template
$this->display('test1');
// Correspondence Myapp/Tpl/default/Index/test1.html
//2 Call Message Modular test2 Operation template ()
$this->display('Message:test2');
// Correspondence Myapp/Tpl/default/Message/test2.html
//3 Call XP Thematic Message Modular test2 Operation template
$this->display('Xp@Message:test2');
// Correspondence Myapp/Tpl/Xp/Message/test2.html
//4 Directly specify the full name of the template file
$this->display('../Message/test3.html');
// Correspondence Myapp/Tpl/default/Message/test3.html

Use judgment statements

We can use if tags to define complex conditional judgments, such as:

<if condition="($name eq 1) OR ($name gt 100) "> value1
<elseif condition="$name eq 2" />value2
<else /> value3
</if>
In condition Property can be supported in the eq Equal judgment expression Same as the comparison tab above, but it is not supported with " > "," < ", because it confuses template parsing, the following usage is wrong: <if condition="$id < 5 "> value1
<else /> value2
</if>

Must be changed to:
<if condition="$id lt 5 "> value1
<else /> value2
</if>

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

<if condition="strtoupper($user['name']) neq 'THINKPHP' "> ThinkPHP
<else /> other Framework
</if>

The condition property can support both point and object syntax, such as:
Automatically determine whether user variables are arrays or objects

<if condition="$user.name neq 'ThinkPHP' "> ThinkPHP
<else /> other Framework
</if>

Or know that the user variable is an object

<if condition="$user:name neq 'ThinkPHP' "> ThinkPHP
<else /> other Framework
</if>

Because the condition attribute of if tag basically uses php syntax, it will be more concise to use judgment tag and Switch tag as much as possible. In principle, those that can be solved by switch and comparison tag should not be completed by if tag as much as possible. Because switch and comparison tags can use variable adjusters and system variables. If the IF tag still fails to meet some special requirements, you can use the native php code or PHP tag to write the code directly.

eq equals (= =)
neq is not equal to (! =)
gt is greater than ( > )
egt is greater than or equal to ( > =)
lt is less than ( < )
elt is less than or equal to ( < =)
heq is always equal to (= = =)
nheq is not always equal to (! = =)
condition condition

Attention
In the condition attribute value, the variable needs the $sign, which is different from other tags.

Methods of using custom functions

Function call format for template variables: {$varnamefunction1function2=arg1, arg2, # # #}

Use example:

{$webTitle|md5|strtoupper|substr=0 , 3} 
{$number|number_format=2}
{$varname|function1|function2=arg1 , arg2 , ### }

Examples are 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, but I can write this in the template
{$vo.cid|cate=###}

Note: Custom functions should be placed in the Project Application Direction/common/common. php. Here is the key.

Description:
There can be no space between {and $symbols, so there is no problem with the space of the following parameters;
# # # Represents the parameter position of the template variable itself;
Support multiple functions, and support spaces between functions;
Support function masking function, and configure the list of prohibited functions in the configuration file;
Support variable caching function, and repeat variable strings without parsing many times.

Readers who are interested in thinkPHP can check the topics of this site: "Introduction to ThinkPHP", "Summary of thinkPHP Template Operation Skills", "Summary of ThinkPHP Common Methods", "Basic Introduction to smarty Template" and "Summary of PHP Template Technology".

I hope this article is helpful to PHP programming based on ThinkPHP framework.


Related articles: