Home Backend Development PHP Tutorial ThinkPHP模板输出display用法分析_php实例

ThinkPHP模板输出display用法分析_php实例

Jun 07, 2016 pm 05:15 PM
display thinkphp template usage output

本文实例分析了ThinkPHP模板输出display用法。分享给大家供大家参考。具体分析如下:

模板变量赋值后就需要调用模板文件来输出相关的变量,模板调用通过display方法来实现,我们在操作方法的最后使用:

复制代码 代码如下:
$this->display();

就可以输出模板,根据前面的模板定义规则,因为系统会按照默认规则自动定位模板文件,所以通常display方法无需带任何参数即可输出对应的模板,这是模板输出的最简单的用法。

事情总有特例,或者根本不需要按模块进行分目录存放,不过display方法总是能够帮你解决问题。

Display方法提供了几种规则让你可以随心所欲的输出需要的模板,无论你的模板文件在什么位置。

下面来看具体的用法:

一、调用当前模块的其他操作模板

格式:display('操作名')

例如,假设当前操作是User模块下面的read操作,我们需要调用User模块的edit操作模版,使用:

复制代码 代码如下:
$this->display('edit');

不需要写模板文件的路径和后缀。

二、调用其他模块的操作模板

格式:display('模块名:操作名')

例如,当前是User模块,我们需要调用Member模块的read操作模版 ,使用:

复制代码 代码如下:
$this->display('Member:read');

这种方式也不需要写模板文件的路径和后缀,严格来说,这里面的模块名和操作名并不一定需要有对应的模块或者操作,只是一个目录名称和文件名称而已,例如,你的项目里面可能根本没有Public模块,更没有Public模块的menu操作,但是一样可以使用

复制代码 代码如下:
$this->display('Public:menu');

输出这个模板文件,理解了这个,模板输出就清晰了.

三、调用其他主题的操作模板

格式:display('主题名:模块名:操作名')

例如我们需要 调用Xp主题的User模块的edit操作模版,使用:

复制代码 代码如下:
$this->display('Xp:User:edit');

这种方式需要指定模块和操作名

四、直接全路径输出模板

格式:display('模板文件名')

例如,我们直接输出当前的Public目录下面的menu.html模板文件,使用:

复制代码 代码如下:
$this->display('./Public/menu.html');

这种方式需要指定模板路径和后缀,这里的Public目录是位于当前项目入口文件位置下面,如果是其他的后缀文件,也支持直接输出,例如:

复制代码 代码如下:
$this->display('./Public/menu.tpl');

只要./Public/menu.tpl是一个实际存在的模板文件,如果使用的是相对路径的话,要注意当前位置是相对于项目的入口文件,而不是模板目录.

事实上,display方法还有其他的参数和用法。

有时候某个模板页面我们需要输出指定的编码,而不是默认的编码,可以使用:

复制代码 代码如下:
$this->display('Member:read', 'gbk');

或者输出的模板文件不是text/html格式的,而是XML格式的,可以用:

复制代码 代码如下:
$this->display('Member:read', 'utf-8', 'text/xml');

如果你的网站输出编码不是默认的编码,可以使用:

复制代码 代码如下:
'DEFAULT_CHARSET'=> 'gbk'

如果要输出XML格式的,可以用:

复制代码 代码如下:
'TMPL_CONTENT_TYPE'=> 'text/xml'

如果不需要渲染模板文件而是直接输出内容,可以使用show方法,例如:

复制代码 代码如下:
$this->show($content, 'utf-8', 'text/xml');

希望本文所述对大家的ThinkPHP框架程序设计有所帮助。

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

Fujifilm X-M5 price expectations rise as leaked selfie display adds complexity to the formerly affordable camera line Fujifilm X-M5 price expectations rise as leaked selfie display adds complexity to the formerly affordable camera line Sep 07, 2024 am 09:34 AM

Fujifilm fans were recently very excited at the prospect of the X-T50, since it presented a relaunch of the budget-oriented Fujifilm X-T30 II that had become quite popular in the sub-$1,000 APS-C category. Unfortunately, as the Fujifilm X-T50's launc

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

How is the performance of thinkphp? How is the performance of thinkphp? Apr 09, 2024 pm 05:24 PM

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

Google Pixel 9 Pro XL ranks 2nd in DxOMark\'s \'Global\' smartphone camera ranking Google Pixel 9 Pro XL ranks 2nd in DxOMark\'s \'Global\' smartphone camera ranking Aug 23, 2024 am 06:42 AM

Google's new Pixel 9 series has introduced a new variant that wasn't present in previous lineups, the Pixel 9 Pro XL. It is essentially identical to the non-XL variant (pre-order at Amazon), but as the name suggests, it has a larger screen. The two e

See all articles