Home  >  Article  >  Backend Development  >  ThinkPHP template output display

ThinkPHP template output display

不言
不言Original
2018-06-08 11:32:002184browse

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 skill. Friends who need it can refer to it

This article analyzes the usage of ThinkPHP template output display in an example. Share it with everyone for your reference. The specific analysis is as follows:

After the template variable is assigned, the template file needs to be called to output the relevant variables. The template call is implemented through the display method. We use it at the end of the operation method:

$this->display();

Output template, according to the previous template definition rules, because the system will automatically locate the template file according to the default rules, so usually the display method can output the corresponding template without any parameters. This is the simplest usage of template output.

There are always exceptions to things, or there may be no need to store them in directories by module at all, but the display method can always help you solve the problem.

The Display method provides several rules so that you can output the template you need as you like, no matter where your template file is.

Let’s look at the specific usage:

1. Call other operation templates of the current module

Format: display('operation name')

For example , Assuming that the current operation is the read operation under the User module, we need to call the edit operation template of the User module, using:

$this->display('edit');

There is no 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, currently it is the User module, we need to call the Member module For the read operation template, use:

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

This method does not require writing the path and suffix of the template file. Strictly speaking, the module name and operation name here do not necessarily need to have corresponding modules or operations. It's just a directory name and file name. For example, your project may not have a Public module at all, and there is no menu operation for the Public module, but you can still use

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

to output this template file. Understand this, template The output will be clear.

3. Call the operation template of other themes

Format: display('topic name: module name: operation name')

For example, we need to call Edit operation template of the User module of the Xp theme, use:

$this->display('Xp:User:edit');

This method requires specifying the module and operation name

4. Direct full path output template

Format: display ('Template file name')

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

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

This method requires specifying the template path and suffix, here The Public directory is located below the current project entry file location. If it is another suffix file, direct output is also supported, for example:

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

As long as ./Public/menu.tpl is an actual existing template file, if If you use a relative path, please note that the current location is relative to the entry file of the project, not the template directory.

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

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

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

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

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

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

'DEFAULT_CHARSET'=> 'gbk'

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

'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:

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

The above is the entire content of this article. I hope it will be helpful to everyone's learning. For more related content, please pay attention to PHP Chinese website!

Related recommendations:

Thinkphp5.0's method of automatically generating modules and directories

ThinkPHP template range judgment output In tag Usage with Range tag

The above is the detailed content of ThinkPHP template output display. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn