本文实例讲述了thinkphp3.x中display方法及show方法的用法。分享给大家供大家参考,具体如下:
通过前面的文章在了解了控制器和模型操作后,我们开始熟悉视图部分,ThinkPHP中的视图主要就是指模板文件和模板引擎,本篇首先了解下模板文件以及是如何进行渲染输出的。
一、模板定义
为了对模板文件更加有效的管理,ThinkPHP对模板文件进行目录划分,默认的模板文件定义规则是:
模板目录/[分组名/][模板主题/]模块名/操作名+模板后缀
模板目录默认是项目下面的Tpl, 当定义分组的情况下,会按照分组名分开子目录,新版模板主题默认是空(表示不启用模板主题功能),
模板主题功能是为了多模板切换而设计的,如果有多个模板主题的话,可以用DEFAULT_THEME参数设置默认的模板主题名。
在每个模板主题下面,是以项目的模块名为目录,然后是每个模块的具体操作模板文件,例如:
User模块的add操作 对应的模板文件就应该是:
Tpl/User/add.html
模板文件的默认后缀是.html,也可以通过TMPL_TEMPLATE_SUFFIX来配置成其他的。例如,我们可以配置:
'TMPL_TEMPLATE_SUFFIX'=>'.tpl'
定义后,User模块的add操作 对应的模板文件就变成是:
Tpl/User/add.tpl
如果项目启用了模块分组功能(假设User模块属于Home分组),那么默认对应的模板文件可能变成 :
Tpl/Home/User/add.html
分组模式下面,如果觉得目录结构太深,可以通过设置TMPL_FILE_DEPR参数来配置简化模板的目录层次,例如设置:
'TMPL_FILE_DEPR'=>'_'
默认的模板文件就变成了:
Tpl/Home/User_add.html
正是因为系统有这样一种模板文件自动识别的规则,所以给我们的模板渲染输出带来了简化。
二、模板渲染
模板定义后就可以通过display和show方法来渲染输出。其中display方法需要我们有定义模板文件,而show方法则是直接渲染内容输出。
最常用的是display方法,调用格式:
第一种:
display('[主题:][模块:][操作]'[,'字符编码'][,'输出类型'])
第二种:
display('完整的模板文件名'[,'字符编码'][,'输出类型'])
下面是一个最典型的用法,不带任何参数:
$this->display();
表示系统会按照默认规则自动定位模板文件,所以通常display方法无需带任何参数即可输出对应的模板,这是模板输出的最简单的用法。
如果没有按照模板定义规则来定义模板文件,或者我需要调用其他模块下面的某个模板,可以使用:
$this->display('edit');
表示调用当前模块下面的edit模板
$this->display('Member:read');
表示调用Member模块下面的read模板。
如果我们使用了模板主题功能,那么也可以支持跨主题调用,使用:
$this->display('theme:User:edit');
表示调用theme主题下面的User模块的edit模板。
这种方式的渲染输出不需要写模板文件的路径和后缀,确切地说,这里面的模块和操作并不一定需要有对应的模块或者操作,只是一个目录名称和文件名称而已,例如,你的项目里面可能根本没有Public模块,更没有Public模块的menu操作,但是一样可以使用
$this->display('Public:menu');
输出这个模板文件。理解了这个,模板输出就清晰了。
display方法支持在渲染输出的时候指定输出编码和类型,例如:
$this->display('read', 'utf-8', 'text/xml');
表示输出XML页面类型(配合你的应用需求可以输出很多类型)。
事情总有特例,如果模板目录是自定义的,或者根本不需要按模块进行分目录存放,那么默认的display渲染规则就不能处理,这个时候,我们就需要使用另外一种方式来应对,直接传入模板文件名即可,例如:
$this->display('./Public/menu.html');
这种方式需要指定模板路径和后缀,这里的Public目录是位于当前项目入口文件位置下面。如果是其他的后缀文件,也支持直接输出,例如:
$this->display('./Public/menu.tpl');
只要./Public/menu.tpl是一个实际存在的模板文件。
要注意模板文件位置是相对于项目的入口文件,而不是模板目录。
还有一种情况是,你需要获取渲染模板的输出内容,就可以使用fetch方法,fetch方法的用法和display基本一致,区别就在于fetch方法渲染后不是直接输出,而是返回渲染后的内容,例如:
$content = $this->fetch('Member:edit');
使用fetch方法获取渲染内容后,你可以进行过滤和替换等操作,用于对模板输出的复杂需求。
如果你没有定义任何模板文件,或者把模板内容存储到数据库中的话,你就需要使用show方法来渲染输出了,show方法的调用格式:
show('渲染内容'[,'字符编码'][,'输出类型'])
例如,
$this->show($content);
也可以指定编码和类型:
$this->show($content, 'utf-8', 'text/xml');
show方法中的内容也可以支持模板解析。
三、模板赋值
我们知道了如何渲染模板输出,但是如果要在模板中输出变量,必须在在控制器中把变量传递给模板,提供了assign方法对模板变量赋值,无论何种变量类型都统一使用assign赋值。
$this->assign('name',$value); //下面的写法是等效的: //$this->name = $value;
assign方法必须在display和show方法之前调用,并且系统只会输出设定的变量,其它变量不会输出(系统变量可以通过特殊的标签输出,可以无需赋值模板变量),一定程度上保证了变量的安全性。
赋值后,就可以在模板文件中输出变量了,如果使用的是内置模板的话,就可以这样输出:
{$name}
如果要同时输出多个模板变量,可以使用下面的方式:
$array['name'] = 'thinkphp'; $array['email'] = 'liu21st@gmail.com'; $array['phone'] = '12335678'; $this->assign($array);
这样,就可以在模板文件中同时输出name、email和phone三个变量。
模板变量的输出根据不同的模板引擎有不同的方法,我们在后面会专门讲解内置模板引擎的用法。如果你使用的是PHP本身作为模板引擎的话 ,就可以直接在模板文件里面输出了:
<?php echo $name.'['.$email.''.$phone.']';?>
如果采用内置的模板引擎,可以使用:
{$name} [ {$email} {$phone} ]
输出同样的内容。
关于更多的模板标签使用,我们会在后面模板标签中详细讲解。
四、模板替换
在进行模板输出之前,系统还可以对渲染的模板结果进行一些模板的特殊字符串替换操作,也就是实现了模板输出的替换和过滤。这个机制可以使得模板文件的定义更加方便,默认的替换规则有:
../Public: 会被替换成当前项目的公共模板目录 通常是 /项目目录/Tpl/当前主题/Public/
__TMPL__: 会替换成项目的模板目录 通常是 /项目目录/Tpl/当前主题/
(注:为了部署安全考虑,../Public和__TMPL__不再建议使用)
__PUBLIC__:会被替换成当前网站的公共目录 通常是 /Public/
__ROOT__: 会替换成当前网站的地址(不含域名)
__APP__: 会替换成当前项目的URL地址 (不含域名)
__GROUP__:会替换成当前分组的URL地址 (不含域名)
__URL__: 会替换成当前模块的URL地址(不含域名)
__ACTION__:会替换成当前操作的URL地址 (不含域名)
__SELF__: 会替换成当前的页面URL
注意这些特殊的字符串是严格区别大小写的,并且这些特殊字符串的替换规则是可以更改或者增加的,我们只需要在项目配置文件中配置TMPL_PARSE_STRING就可以完成。如果有相同的数组索引,就会更改系统的默认规则。例如:
'TMPL_PARSE_STRING' =>array( '__PUBLIC__' => '/Common', // 更改默认的/Public 替换规则 '__JS__' => '/Public/JS/', // 增加新的JS类库路径替换规则 '/Uploads' => '/Uploads', // 增加新的上传路径替换规则 )
有了模板替换规则后,页面上所有的__PUBLIC__ 字符串都会被替换,那如果确实需要输出__PUBLIC__ 字符串到模板呢,我们可以通过增加替换规则的方式,例如:
'TMPL_PARSE_STRING' =>array( '--PUBLIC--' => '__PUBLIC__', // 采用新规则输出/Public字符串 )
这样增加替换规则后,如果我们要输出__PUBLIC__ 字符串,只需要在模板中添加--PUBLIC--,其他替换字符串的输出方式类似。
五、总结
通过本篇的学习,我们大概掌握了如何定义模板文件和进行模板渲染输出,以及如何赋值模板变量,后面我们将会学习如何在模板文件中使用标签来简化你的书写。
PS:这里推荐几款本站的格式化美化工具,相信大家在以后的开发中能够用得上:
php代码在线格式化美化工具:
http://tools.php.net/code/phpformat
JavaScript代码美化/压缩/格式化/加密工具:
http://tools.php.net/code/jscompress
在线XML格式化/压缩工具:
http://tools.php.net/code/xmlformat
JSON代码格式化美化工具:
http://tools.php.net/code/json
在线XML/JSON互相转换工具:
http://tools.php.net/code/xmljson
sql代码在线格式化美化工具:
http://tools.php.net/code/sqlcodeformat
更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《smarty模板入门基础教程》及《PHP模板技术总结》。
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
