search
HomeBackend DevelopmentPHP TutorialThinkPHP模板输出display用法分析_php实例

本文实例分析了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
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
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool