Home >Backend Development >PHP Tutorial >ThinkPHP template output display usage analysis, thinkphpdisplay_PHP tutorial
This article analyzes the usage of ThinkPHP template output display through examples. Share it with everyone for your reference. The specific analysis is as follows:
After assigning values to template variables, you need to call the template file to output the relevant variables. The template call is implemented through the display method. We use it at the end of the operation method:
can output the 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 the current operation is the read operation under the User module, we need to call the edit operation template of the User module, use:
No need to write the path and suffix of the template file.
2. Call operation templates of other modules
Format: display('module name: operation name')
For example, currently it is the User module, we need to call the read operation template of the Member module, use:
This method also 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, but are just a directory name and file name. For example, Your project may not have a Public module at all, let alone the menu operation of the Public module, but you can still use it
Output this template file. Once you understand this, the template output will be clear.
3. Call operation templates of other themes
Format: display('theme name: module name: operation name')
For example, if we need to call the edit operation template of the User module of the Xp theme, use:
This method requires specifying the module and operation name
4. Direct full path output template
Format: display('template file name')
For example, if we directly output the menu.html template file under the current Public directory, use:
This method requires specifying the template path and suffix. The Public directory here is located below the current project entry file location. If it is another suffix file, direct output is also supported, for example:
As long as ./Public/menu.tpl is an actual template file, if a relative path is used, 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 usage.
Sometimes we need to output a specified encoding for a template page instead of the default encoding. You can use:
Or the output template file is not in text/html format, but in XML format. You can use:
If your website output encoding is not the default encoding, you can use:
If you want to output XML format, you can use:
If you do not need to render the template file but directly output the content, you can use the show method, for example:
I hope this article will be helpful to everyone’s ThinkPHP framework programming.