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

WBOY
Release: 2016-07-12 08:52:20
Original
1437 people have browsed it

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'

Copy after login
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'=>'_'

Copy after login

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();

Copy after login

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'); 

Copy after login

means calling the edit template under the current module

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

Copy after login

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'); 

Copy after login

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'); 

Copy after login

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'); 

Copy after login

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');

Copy after login

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');

Copy after login

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');

Copy after login

使用fetch方法获取渲染内容后,你可以进行过滤和替换等操作,用于对模板输出的复杂需求。

如果你没有定义任何模板文件,或者把模板内容存储到数据库中的话,你就需要使用show方法来渲染输出了,show方法的调用格式:

show('渲染内容'[,'字符编码'][,'输出类型'])

例如,

$this->show($content);

Copy after login

也可以指定编码和类型:

$this->show($content, 'utf-8', 'text/xml'); 

Copy after login

show方法中的内容也可以支持模板解析。

三、模板赋值

我们知道了如何渲染模板输出,但是如果要在模板中输出变量,必须在在控制器中把变量传递给模板,提供了assign方法对模板变量赋值,无论何种变量类型都统一使用assign赋值。

$this->assign('name',$value);
//下面的写法是等效的:
//$this->name = $value;

Copy after login

assign方法必须在display和show方法之前调用,并且系统只会输出设定的变量,其它变量不会输出(系统变量可以通过特殊的标签输出,可以无需赋值模板变量),一定程度上保证了变量的安全性。

赋值后,就可以在模板文件中输出变量了,如果使用的是内置模板的话,就可以这样输出:

{$name}

Copy after login

如果要同时输出多个模板变量,可以使用下面的方式:

$array['name'] = 'thinkphp'; 
$array['email'] = 'liu21st@gmail.com'; 
$array['phone'] = '12335678'; 
$this->assign($array);

Copy after login

这样,就可以在模板文件中同时输出name、email和phone三个变量。

模板变量的输出根据不同的模板引擎有不同的方法,我们在后面会专门讲解内置模板引擎的用法。如果你使用的是PHP本身作为模板引擎的话 ,就可以直接在模板文件里面输出了:

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

Copy after login

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

{$name} [ {$email} {$phone} ]

Copy after login

输出同样的内容。

关于更多的模板标签使用,我们会在后面模板标签中详细讲解。

四、模板替换

在进行模板输出之前,系统还可以对渲染的模板结果进行一些模板的特殊字符串替换操作,也就是实现了模板输出的替换和过滤。这个机制可以使得模板文件的定义更加方便,默认的替换规则有:

../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', // 增加新的上传路径替换规则 
)

Copy after login

有了模板替换规则后,页面上所有的__PUBLIC__ 字符串都会被替换,那如果确实需要输出__PUBLIC__ 字符串到模板呢,我们可以通过增加替换规则的方式,例如:

'TMPL_PARSE_STRING' =>array( 
  '--PUBLIC--' => '__PUBLIC__', // 采用新规则输出/Public字符串 
)

Copy after login

这样增加替换规则后,如果我们要输出__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,...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!