tp3.1 (2)

Source: Internet
Author: User

One, Custom function

1, U () URL

Configuration data for 2, C () conf

3. Custom Functions

In the user's application, common-"Create common.php, then write your own method and use it in the controller,

4, dump () print array is more harmonious than var_dump ()

II. Create a project template

Create a template in the TPL

Create the index (presentation method) folder and create the index.html file in index

The template can be directly connected in the index controller ($this->display)

This is the index method of the index controller.

To enable the test method to display the index controller

$this->display (' index/test ')

or $this->display (' test ')

Iii. assignment and output of templates

1th:

In the Controller->index ()

$name = "Doson";

In tpl->index->index.html

So how to accept the value of $name: <?php echo $name?>

2nd:

In the Controller->index ()

$this-Assign (' name ', $name);

In tpl->index->index.html

So how to accept the value of $name: <?php echo $name?>

When using the second way: can be infinitely assigned, and the first method can only be assigned to a number of $this->assign (' name ', ' Zhang San ')->assign (' age ', 12);

Iv. template engine for TP

Thinkphp built-in engine class: thinkphp\lib\template\thinktemplate.class.php

Five, the template engine variable output and operation

In the Controller->index ()

$me [' name ']= ' yyyyy ';

$me [' Age ']=12;

$this->assign (' Me ', $me);

In the tpl->index->index.html:

Method One: {$me [' name ']}

Method Two:

{$me. Name}

Comment Method: {//$me. Name}

A value that is undefined can be set to a default value

{$me. sex|default= ' Man '}

To perform operations on a template

The operation must be written in the form of an array {$me [' Age ']+1}

VI: thinkphp function and system parameters of template engine

Method one: In controller-"index

echo $name =substr (MD5 ($me [' name ']), 0,5);

Method Two: In tpl-"index-" index

{$me [' name ']|md5|substr=0,5}

Call of Time

In controller-"index

$now =time ();

In tpl-"index-" index

{$now |date= ' y-m-d h:i:s ', # # #} where # # #表示的是变量, that is, $now

{$Think. Now}

system function {$Think. now}
{$Think. Version}
{$Think. Server.http_host}

Vii. volist and foreach loops of the thinkphp template engine

In the Contoller:

$person =array (
1=>array (' name ' = ' jack6 ', ' age ' = ' 12 '),
2=>array (' name ' = ' jack5 ', ' age ' = ' 15 '),
3=>array (' name ' = ' jack4 ', ' age ' = ' 12 '),
4=>array (' name ' = ' jack1 ', ' age ' = ' 11 '),
5=>array (' name ' = ' jack2 ', ' age ' = ' 15 '),
);
$this->assign (' person ', $person);
$this->display ();

In the TPL:

<volist name= "Person" id= "Data" >
{$data [' name '}}-----{$data [' age ']}<br/>
</volist>
From the second start, Loop 3
<volist name= "Person" id= "Data" offset= ' 1 ' length= ' 3 ' >
{$data [' name '}}-----{$data [' age ']}<br/>
</volist>

Set default values

<volist name= "Person" id= "Data" offset= ' 1 ' length= ' 3 ' empty= ' I have no statistics ' >
{$data [' name '}}-----{$data [' age ']}<br/>
</volist>

Foreach

<foreach name= ' person ' item= ' data ' >
{$data [' name '}}------{$data [' age ']}<br/>
</foreach>

Viii. thinkphp for Loop

eq= neq!= gt> egt>= lt<

elt<= Heq = = = nheq!==

Controller

: for ($i =0; $i <10; $i + +) {
echo $i;
}

Template

<for start= ' 1 ' end= ' comparision= ' elt ' name= ' K ' >
{$k}<br/>
</for>

Ix. if judgment of TP template engine

In the controller:

$num =10; $this->assign (' num ', $num);

In the TPL:

<if condition= "$num gt ten" >num greater than 10

<elseif condition= "$num lt ten"/>num less than 10

<else condition= "$num eq ten"/>num equals 10

</if>

The output is: num=10

X. SWITCH judgment of TP template engine

In the controller:

$name = ' Xiaohong '; $this->assign (' name ', $name);

In the TPL:

<switch name= "Name" >

<case value= "Xiaohong|" > Xiaoming, get out of the way </case>

<case value= "Laoshi" > Xiao Ming roll </case><defalut/> xiaoming, fuck off.

</switch>

Comparison tab of the TP template engine

< comparison label name= "variable" value= "value of comparison" >

</Compare Tags >

Method One,

<eq name= "num" value= "ten" >num=10</eq>

<NEQ name= "num" value= "ten" > Num!=10</neq>

Method Two,

<eq name= "num" value= "ten" >num=10 <else/>num!=10</eq>

Method Three,

<compare name= "num" value= "ten" type= "EQ" >num=10<else/>num!=10</compare>

12. TP template Engine's interval label

In/notin

Method One,

<in name= "num" value= "1,2,4,7" > in range </in>
<notin name= "num" value= "1,2,4,7" > Not in range </notin>

Method Two,
<in name= "num" value= "2,4,7" > in range <else/> not in this range </in>

Method Three,

<range name= ' num ' value= ' 12,2,4 ' type= ' in ' > has 2 this number <else/> no </range>
Between/notbetween: Judging interval range I

Method One,

<between name= ' num ' value= ' 1,19 ' > is really here </between><br/>
<notbetween name= ' num ' value= ' 13,9 ' >bu really here </notbetween>
<br/>

Method Two,
<between name= ' num ' value= ' 1,19 ' > is really here <else/> no longer </between><br/>

13, ternary operator

{$num >17? ' Greater than 17 ': ' Not greater than 17 '}

14. Template Combat

Class Indexaction extends Action {
Public Function index () {
$person =array (
1=>array (' name ' = ' jack1 ', ' age ' = ' 16 '),
2=>array (' name ' = ' jack2 ', ' age ' = ' 17 '),
3=>array (' name ' = ' jack3 ', ' age ' = ' 18 '),
4=>array (' name ' = ' jack4 ', ' age ' = ' 19 '),
);
$this->assign (' person ', $person);
$this->display ();
}

<foreach name= ' person ' item= ' data ' >
<egt name= ' data.age ' value= ' >{$data. Name} has grown up.
<else/>{$data. Name} is also a child </egt>
</foreach>

Introduce Php:<php>echo $person [1][' name ']</php> in the template

The debugging method of XV and thinkphp

Method One,

In the app-"conf-" debug.php, in the debug.php file can change the value of config.php to debug

Method Two,

Debugging specifically for developers

Turn on trace information for a page

In the debug.php file: ' Show_page_trace ' =>true,

Method Three,

Trace (' Name ', C (' name '));

Method Four,

Dump ($_server);

Method Five,

G (' run ');

for ($i =0; $i <1000; $i + +) {$count + = $i;}

Echo G (' Run ', ' end ');//units: milliseconds

Do the programming, complex logic: view dead loops, diffuse loops

tp3.1 (2)

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.