search
HomeBackend DevelopmentPHP TutorialUsage examples of display method and show method in thinkphp3.x, thinkphp3.xdisplay_PHP tutorial

Usage examples of display method and show method in thinkphp3.x, thinkphp3.xdisplay

This article describes the usage of display method and show method in thinkphp3.x. Share it with everyone for your reference, the details are as follows:

After understanding the controller and model operations through the previous article, we began to become familiar with the view part. The view in ThinkPHP mainly refers to the template file and template engine. In this article, we first understand the template file and how to render the output. .

1. Template definition

In order to manage template files more effectively, ThinkPHP divides template files into directories. The default template file definition rules are:

Template directory/[Group name/][Template theme/]Module name/Operation name Template suffix

The template directory defaults to the Tpl under the project. When groups are defined, subdirectories will be separated according to group names. The new version of the template theme is empty by default (indicating that the template theme function is not enabled),

The template theme function is designed for multi-template switching. If there are multiple template themes, you can use the DEFAULT_THEME parameter to set the default template theme name.

Under each template theme, there is a directory with the module name of the project, and then the specific operation template file of each module, for example:

The corresponding template file for the add operation of the User module should be:

Tpl/User/add.html

The default suffix of the template file is .html, and it can also be configured to other ones through TMPL_TEMPLATE_SUFFIX. For example, we can configure:

'TMPL_TEMPLATE_SUFFIX'=>'.tpl'

After

is defined, the template file corresponding to the add operation of the User module becomes:

Tpl/User/add.tpl

If the project has the module grouping function enabled (assuming that the User module belongs to the Home group), then the default corresponding template file may become:

Tpl/Home/User/add.html

In group mode, if you feel that the directory structure is too deep, you can configure the directory hierarchy of the simplified template by setting the TMPL_FILE_DEPR parameter, for example, set:

'TMPL_FILE_DEPR'=>'_'

The default template file becomes:

Tpl/Home/User_add.html

It is precisely because the system has such a rule for automatically identifying template files that it simplifies our template rendering output.

2. Template rendering

After the template is defined, the output can be rendered through the display and show methods. The display method requires us to define a template file, while the show method directly renders the content output.

The most commonly used is the display method, the calling format:

First type:

display('[Topic:][Module:][Operation]'[,'Character encoding'][,'Output type'])

Second type:

display('Complete template file name'[,'Character encoding'][,'Output type'])

The following is the most typical usage, without any parameters:

$this->display();

means that 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.

If the template file is not defined according to the template definition rules, or I need to call a template under other modules, I can use:

$this->display('edit'); 

means calling the edit template under the current module

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

means calling the read template under the Member module.

If we use the template theme function, we can also support cross-theme calls, use:

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

indicates calling the edit template of the User module under the theme.

Rendering output in this way does not need to write the path and suffix of the template file. To be precise, the modules and operations here do not necessarily need to have corresponding modules or operations, it is 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

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

Export this template file. After understanding this, the template output will be clear.

The display method supports specifying the output encoding and type when rendering output, for example:

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

indicates the output XML page type (many types can be output to suit your application needs).

There are always exceptions to things. If the template directory is customized, or does not need to be stored in sub-directories by module, then the default display rendering rules cannot handle it. At this time, we need to use another way to deal with it. , just pass in the template file name directly, for example:

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

This method requires specifying the template path and suffix. The Public directory here is located below the location of the current project entry file. 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.

Please note that the template file location is relative to the entry file of the project, not the template directory.

还有一种情况是,你需要获取渲染模板的输出内容,就可以使用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本身作为模板引擎的话 ,就可以直接在模板文件里面输出了:

<&#63;php echo $name.'['.$email.''.$phone.']';&#63;>

如果采用内置的模板引擎,可以使用:

{$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.jb51.net/code/phpformat

JavaScript code beautification/compression/formatting/encryption tool:
http://tools.jb51.net/code/jscompress

Online XML formatting/compression tool:
http://tools.jb51.net/code/xmlformat

JSON code formatting and beautification tool:
http://tools.jb51.net/code/json

Online XML/JSON conversion tool:
http://tools.jb51.net/code/xmljson

SQL code online formatting and beautification tool:
http://tools.jb51.net/code/sqlcodeformat

Readers who are interested in more thinkPHP-related content can check out the special topics on this site: "ThinkPHP Getting Started Tutorial", "ThinkPHP Common Methods Summary", "Smarty Template Basic Tutorial" and "PHP Template Technology Summary".

I hope this article will be helpful to everyone’s PHP programming based on the ThinkPHP framework.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1127838.htmlTechArticleUsage examples of display method and show method in thinkphp3.x, thinkphp3.xdisplay This article describes the usage examples in thinkphp3.x Usage of display method and show method. Sharing it with everyone for your reference,...
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
thinkphp是不是国产框架thinkphp是不是国产框架Sep 26, 2022 pm 05:11 PM

thinkphp是国产框架。ThinkPHP是一个快速、兼容而且简单的轻量级国产PHP开发框架,是为了简化企业级应用开发和敏捷WEB应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。

一起聊聊thinkphp6使用think-queue实现普通队列和延迟队列一起聊聊thinkphp6使用think-queue实现普通队列和延迟队列Apr 20, 2022 pm 01:07 PM

本篇文章给大家带来了关于thinkphp的相关知识,其中主要介绍了关于使用think-queue来实现普通队列和延迟队列的相关内容,think-queue是thinkphp官方提供的一个消息队列服务,下面一起来看一下,希望对大家有帮助。

thinkphp的mvc分别指什么thinkphp的mvc分别指什么Jun 21, 2022 am 11:11 AM

thinkphp基于的mvc分别是指:1、m是model的缩写,表示模型,用于数据处理;2、v是view的缩写,表示视图,由View类和模板文件组成;3、c是controller的缩写,表示控制器,用于逻辑处理。mvc设计模式是一种编程思想,是一种将应用程序的逻辑层和表现层进行分离的方法。

实例详解thinkphp6使用jwt认证实例详解thinkphp6使用jwt认证Jun 24, 2022 pm 12:57 PM

本篇文章给大家带来了关于thinkphp的相关知识,其中主要介绍了使用jwt认证的问题,下面一起来看一下,希望对大家有帮助。

thinkphp扩展插件有哪些thinkphp扩展插件有哪些Jun 13, 2022 pm 05:45 PM

thinkphp扩展有:1、think-migration,是一种数据库迁移工具;2、think-orm,是一种ORM类库扩展;3、think-oracle,是一种Oracle驱动扩展;4、think-mongo,一种MongoDb扩展;5、think-soar,一种SQL语句优化扩展;6、porter,一种数据库管理工具;7、tp-jwt-auth,一个jwt身份验证扩展包。

一文教你ThinkPHP使用think-queue实现redis消息队列一文教你ThinkPHP使用think-queue实现redis消息队列Jun 28, 2022 pm 03:33 PM

本篇文章给大家带来了关于ThinkPHP的相关知识,其中主要整理了使用think-queue实现redis消息队列的相关问题,下面一起来看一下,希望对大家有帮助。

thinkphp 怎么查询库是否存在thinkphp 怎么查询库是否存在Dec 05, 2022 am 09:40 AM

thinkphp查询库是否存在的方法:1、打开相应的tp文件;2、通过“ $isTable=db()->query('SHOW TABLES LIKE '."'".$data['table_name']."'");if($isTable){...}else{...}”方式验证表是否存在即可。

thinkphp3.2怎么关闭调试模式thinkphp3.2怎么关闭调试模式Apr 25, 2022 am 10:13 AM

在thinkphp3.2中,可以利用define关闭调试模式,该标签用于变量和常量的定义,将入口文件中定义调试模式设为FALSE即可,语法为“define('APP_DEBUG', false);”;开启调试模式将参数值设置为true即可。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

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

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),