thinkphp Template Output Display usage Analysis _php example

Source: Internet
Author: User

The example of thinkphp template output display usage is analyzed in this paper. Share to everyone for your reference. The specific analysis is as follows:

After the template variable is assigned, you need to call the template file to output the related variables, and the template calls are implemented by the display method, which we use at the end of the operation method:

Copy Code code as follows:
$this->display ();

You can output the template, based on the previous template definition rules, because the system will automatically locate the template file according to the default rules, so usually the display method with no parameters to output the corresponding template, which is the simplest use of template output.

There's always a special case, or there's no need to sort by module, but the display method can always help you solve the problem.

The display method provides several rules that allow you to output the required templates wherever you want, regardless of where your template files are.

Here are some specific uses:

Another action template that invokes the current module

Format: Display (' Operation name ')

For example, assuming the current operation is the read operation under the user module, we need to invoke the Edit action template for the user module, using:

Copy Code code as follows:
$this->display (' edit ');

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

Second, the operation template to invoke other modules

Format: Display (' Module name: Operation name ')

For example, currently the user module, we need to invoke the Read action template of the member module, using:

Copy Code code as follows:
$this->display (' Member:read ');

This way also does not need to write template file path and suffix, strictly speaking, this module name and operation name does not necessarily need to have a corresponding module or operation, just a directory name and file name, for example, your project may not have the public module, There is no menu action for the public module, but you can use the

Copy Code code as follows:
$this->display (' Public:menu ');

Output This template file, understand this, the template output is clear.

Third, invoke the action template of other topics

Format: Display (' Subject name: module Name: Operation name ')

For example, we need to invoke the Edit action template for the user module of the XP theme, using:

Copy Code code as follows:
$this->display (' Xp:User:edit ');

This way you need to specify the module and operation name

Four, direct full path output template

Format: Display (' template filename ')

For example, we directly output the menu.html template file below the current public directory, using:

Copy Code code as follows:
$this->display ('./public/menu.html ');

This way you need to specify the template path and suffix, where the public directory is located under the current project portal file location and, if it is a different suffix file, it also supports direct output, for example:

Copy Code code as follows:
$this->display ('./public/menu.tpl ');

As long as./PUBLIC/MENU.TPL is an actual template file, if you are using a relative path, be aware that the current position is relative to the project's entry file, not the template directory.

In fact, there are other parameters and usages of the display method.

Sometimes a template page we need to output the specified encoding, rather than the default encoding, you can use:

Copy Code code 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:

Copy Code code as follows:
$this->display (' Member:read ', ' utf-8 ', ' text/xml ');

If your site output encoding is not the default encoding, you can use:

Copy Code code as follows:
' Default_charset ' => ' GBK '

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

Copy Code code 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:

Copy Code code as follows:
$this->show ($content, ' utf-8 ', ' text/xml ');

I hope this article will be helpful to everyone's thinkphp framework program design.

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.