ThinkPHP output Template

Source: Internet
Author: User
ThinkPHP has its own output system. if [Lib \ Action] is used to control the C layer, the corresponding V layer is stored in the [Tpl \ default] folder, there are also common naming rules-& gt;

ThinkPHP output Template

1) thinking logic:

ThinkPHP has its own output system. if [Lib \ Action] is used to control the C layer, the corresponding V layer is stored in the [Tpl \ default] folder, there are also common naming rules->

1. each Action corresponds to a folder;

2. Each processing method corresponds to an html page.

For example, there is an Action named Test. according to the naming rules, it must be created in [Lib \ Action] and named TestAction. class. php. The Action Test contains a processing method showpage (). to access this function, you can use http: // 127.0.0.1/myapp2/test/showpage.

First, you must create a file named "test" under [Tpl \ default], and then create a file named "showpage.html" in the file folder. In this case, write at the end of the showpage () method

$ This-> display ();

This method. When http: // 127.0.0.1/myapp2/test/showpage is entered in the browser, it is automatically redirected to [Tpl \ defualt \ test \ showpage.html.

TestAction. class. php <=> Tpl \ defualt \ test

Showpage () <=> showpage.html

2) Application:

Some people may ask, even if this template is available, why?

This is my first question. After learning ThinkPHP, we know that ThinkPHP is another outstanding feature. Custom tags are added to the template.

By using custom tags, you can easily create a V-layer interface and avoid code obfuscation. Very easy to use. Next, we will use an example to describe its simplest and most widely used function-convenient dataset.

Create the showpage () method in TestAction. class. php:

/**

+ ----------------------------------------------------------

* Test template output showpage.html

+ ----------------------------------------------------------

*/

Publicfunctionshowpage ()

{

// 1. transmit a single value for display on the display layer.

$ Value = 'Hello, thinkphp ';

$ This-> assign ('name', $ value );

// 2. transmit multiple values for display on the display layer.

$ Array = array ();

$ Array ['name'] = 'thinkphp ';

$ Array ['email '] = 'liu21st @ gmail.com ';

$ Array ['phone'] = '000000 ';

$ This-> assign ('array', $ array );

// 3. access the database, obtain the database set, and display it on the display layer.

$ Demo = newModel ('demo'); // instantiate the model class

$ List = $ Demo-> select (); // query data

$ This-> assign ('list', $ list); // template variable assignment

$ This-> display ();

}

Create a new file named showpage.html under tpl \ defualt \ test.

ThisisTheShowPage.

Test output: {$ name}

{$ Vo}

No.: {$ vo2.id}

Title: {$ vo2.title}

Content: {$ vo2.content}

Brief description:

1. you can pass the value to the V layer in the form of $ this-> assign ('key value', $ variable );

2. access non-array variables in the form of {$ Key value} on the V layer

3. traverse the array in this form

4. The result is the same as the result.

4. remove "page Trace information"

After the page is displayed as a template, a set of text information with the title "Trace" is returned at the end. It is really good to have this stuff during development, but it is not embarrassing to continue to appear when it is delivered to the customer.

It's easy to get rid of it. in the Conf \ config. php file

'App _ debug' => true, // enable the debugging mode

Just comment it out.

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.