ThinkPHP template output display Usage Analysis _ php instance

Source: Internet
Author: User
This article mainly introduces the usage of ThinkPHP template output display, and analyzes in detail the usage of ThinkPHP using display to call various template outputs in the form of examples. It is a very common practical technique, for more information about the ThinkPHP template output display usage, see the examples in this article. Share it with you for your reference. The specific analysis is as follows:

After the template variable value is assigned, you need to call the template file to output the relevant variables. The template call is implemented through the display method. We use the following method at the end of the operation:

The Code is as follows:

$ This-> display ();

You can output the template and define the rules according to the previous template. Because the system will automatically locate the template file according to the default rules, the display method usually outputs the corresponding template without any parameters, this is the simplest use of template output.

There are always special cases, or there is no need to store sub-directories by module, but the display method can always help you solve the problem.

The Display method provides several rules for you to output the desired template as you like, regardless of the location of your template file.

The specific usage is as follows:

1. Call other operation templates of the current Module

Format: display ('Operation name ')

For example, if the current operation is the read operation under the User module, we need to call the edit operation template of the User module and use:

The Code is as follows:

$ This-> display ('edit ');

You do not need to write the path and suffix of the template file.

2. Call the operation templates of other modules

Format: display ('module name: Operation name ')

For example, if the User module is used, call the read operation template of the Member module and use:

The Code is as follows:

$ This-> display ('Member: read ');

In this way, you do not need to write the path and suffix of the template file. Strictly speaking, the module name and operation name do not necessarily need to have corresponding modules or operations, it's just a directory name and file name. For example, there may be no Public module in your project, and there is no menu operation for the Public module, but it can be used in the same way.

The Code is as follows:

$ This-> display ('Public: menu ');

Output this template file. After understanding this, the template output will be clear.

3. Call the operation templates of other themes

Format: display ('topic name: Module name: Operation name ')

For example, we need to call the edit operation template of the User module of the Xp topic, and use:

The Code is as follows:

$ This-> display ('xp: User: edit ');

In this way, you must specify the module and operation name.

Iv. Direct full-path output Template

Format: display ('template filename ')

For example, we directly output the menu.html template file under the current publicdirectory, using:

The Code is as follows:

$ This-> display ('./Public/menu.html ');

In this way, you need to specify the template path and suffix. The Public directory is located under the File Location of the current project portal. If it is another suffix file, it also supports direct output. For example:

The Code is as follows:

$ This-> display ('./Public/menu. tpl ');

As long as./Public/menu. tpl is an existing template file, if you are using a relative path, note that the current location is relative to the project's portal file, rather than the template directory.

In fact, the display method has other parameters and usage.

Sometimes we need to output the specified encoding on a template page, instead of the default encoding. You can use:

The Code is as follows:

$ This-> display ('Member: read', 'gbk ');

Or the output template file is not in text/html format, but in XML format. You can use:

The Code is as follows:

$ This-> display ('Member: read', 'utf-8', 'text/xml ');

If your website output code is not the default code, you can use:

The Code is as follows:

'Default _ charset' => 'gbk'

If you want to output the XML format, you can use:

The Code is as follows:

'Tmpl _ CONTENT_TYPE '=> 'text/xml'

If you do not need to render the template file but directly output the content, you can use the show method, for example:

The Code is as follows:

$ This-> show ($ content, 'utf-8', 'text/xml ');

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.