Examples of display and show methods in thinkphp3.x

Source: Internet
Author: User
This article mainly introduces the usage of the display method and show method in thinkphp3.x, and analyzes the functions, definitions, assignments, rendering and output techniques of the thinkPHP3.x template based on examples, for more information about how to use the display and show methods in thinkphp3.x, see the examples in this article. We will share this with you for your reference. The details are as follows:

After learning about the controller and model operations in the previous article, we started to get familiar with the view section. the view in ThinkPHP mainly refers to the template file and template engine, this article first describes the template file and how to render the output.

I. Template definition

To manage template files more effectively, ThinkPHP classifies template files into directories. the default template file definition rules are as follows:

Template directory/[group name/] [template topic/] module name/operation name + template suffix

The template directory is the Tpl under the project by default. when a group is defined, the subdirectories are separated by the group name. The new template topic is empty by default (indicating that the template topic function is not enabled ),

The template topic function is designed to switch between multiple templates. if there are multiple template themes, you can use the DEFAULT_THEME parameter to set the default template topic name.

Under each template topic, the project module is named directory, and then the specific operation template file of each module, for example:

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

Tpl/User/add.html

The suffix of the template file is .html. you can also use TMPL_TEMPLATE_SUFFIX to configure it as another one. For example, we can configure:

'TMPL_TEMPLATE_SUFFIX'=>'.tpl'

After definition, the template file corresponding to the add operation of the User module becomes:

Tpl/User/add. tpl

If the project enables the module grouping function (assuming that the User module belongs to the Home group), the default template file may be:

Tpl/Home/User/add.html

In the group mode, if you think the directory structure is too deep, you can set the TMPL_FILE_DEPR parameter to simplify the directory hierarchy of the template, for example:

'TMPL_FILE_DEPR'=>'_'

The default template file is changed:

Tpl/Home/User_add.html

Because the system has such a rule for automatic template file identification, it simplifies the template rendering output.

II. Template rendering

After the template is defined, you can use the display and show methods to render the output. The display method requires a template file, while the show method directly renders the content output.

The most common method is the display method. call format:

First:

Display ('[topic:] [Module:] [operation]' [, 'Encoding '] [, 'output type'])

Second:

Display ('complete template filename '[, 'character encoding'] [, 'output type'])

The following is a typical usage without any parameters:

$this->display();

The system will automatically locate the template file according to the default rules. Therefore, the display method usually outputs the corresponding template without any parameters. this is the simplest use of template output.

If the template file is not defined according to the template definition rules, or you need to call a template under another module, you can use:

$this->display('edit'); 

Indicates calling the edit template under the current module.

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

Indicates calling the read template under the Member module.

If the template topic function is used, cross-topic calling is also supported. use:

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

Calls the edit template of the User module under the theme topic.

In this way, the rendering output does not need to write the path and suffix of the template file. Specifically, the modules and operations here do not necessarily need corresponding modules or operations, it's just a directory name and file name. for example, there may be no Public module in your project, and there is no menu operation for the Public module, but it can be used in the same way.

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

Output this template file. With this understanding, the template output is clear.

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

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

Output XML page type (many types can be output in combination with your application requirements ).

There is always a special case. if the template directory is customized or does not need to be stored in sub-directories by module, the default display rendering rule cannot be processed. at this time, you can directly input the template file name, for example:

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

In this way, you must specify the template path and suffix. The Public directory is located under the file location of the current project portal. Other suffix files also support direct output, for example:

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

As long as./Public/menu. tpl is an existing template file.

Note that the template file location is relative to the project's entry file, rather than the template directory.

Another case is that you need to obtain the output content of the rendering template, and you can use the fetch method. the usage of the fetch method is basically the same as that of the display method. The difference is that the fetch method does not directly output the rendering, instead, it returns the rendered content, for example:

$content = $this->fetch('Member:edit');

After obtaining the rendering content using the fetch method, you can filter and replace the rendering content for the complex requirements of template output.

If you do not define any template file or store the template content in the database, you need to use the show method to render the output. The Call Format of the show method is as follows:

Show ('rendered content' [, 'character encoding'] [, 'output type'])

For example,

$this->show($content);

You can also specify the encoding and type:

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

The content in the show method can also support template parsing.

III. Template assignment

We know how to render template output, but if you want to output variables in the template, you must pass the variables to the template in the controller, and provide the assign method to assign values to the template variables, all variable types use assign assignment.

$ This-> assign ('name', $ value); // The following statements are equivalent: // $ this-> name = $ value;

The assign method must be called before the display and show methods, and the system only outputs the set variables. other variables are not output (system variables can be output through special labels, you do not need to assign values to template variables. to a certain extent, this ensures the security of variables.

After assigning values, you can output the variables in the template file. if you use a built-in template, you can output the variables as follows:

{$name}

To output multiple Template variables at the same time, use the following method:

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

In this way, you can output the name, email, and phone variables in the template file at the same time.

The template variable output has different methods based on different template engines. we will detail the usage of the built-in template engine later. If you are using PHP itself as the template engine, you can directly output it in the template file:

<?php echo $name.'['.$email.''.$phone.']';?>

If you use the built-in template engine, you can use:

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

Output the same content.

For more information about the use of template tags, we will explain in detail in the following template tags.

IV. Template replacement

Before template output, the system can perform some special string replacement operations on the rendered template results, that is, replacing and filtering the template output. This mechanism makes it easier to define template files. the default replacement rules include:

/Cms/tpl/Index/Public: it will be replaced with the Public Template directory of the current project, which is usually/project directory/Tpl/current topic/Public/

/Cms/tpl/Index/: The Template directory to be replaced with the project is usually/project directory/Tpl/current topic/

(Note: For the sake of deployment security,/cms/tpl/Index/Public and/cms/tpl/Index/are no longer recommended)

/Public: the Public directory of the current website is usually/Public/

: It will be replaced with the address of the current website (excluding the domain name)

/Index. php: it will be replaced with the URL address of the current project (excluding the domain name)

/Index. php: it will be replaced with the URL address of the current group (excluding the domain name)

/Index. php/Article: it will be replaced with the URL address of the current module (excluding the domain name)

/Index. php/Article/detail: it will be replaced with the URL address of the current operation (excluding the domain name)

/Php-weizijiaocheng-49254.html: will be replaced with the current page URL

Note that these special strings are case-sensitive and the replacement rules of these special strings can be changed or added. you only need to configure TMPL_PARSE_STRING in the project configuration file. If the same array index exists, the system's default rules are changed. For example:

'Tmpl _ PARSE_STRING '=> array ('/public' => '/common ', // change the default/Public replacement rule '_ JS _' => '/Public/JS /', // add a new JS class library path replacement rule '/Uploads' => '/Uploads', // add a new Upload path replacement rule)

With the template replacement rules, all the/Public strings on the page will be replaced. if you need to output the/Public string to the template, we can add a replacement rule, for example:

'Tmpl _ PARSE_STRING '=> array (' -- PUBLIC -- '=>'/public', // use the new rule to output/Public string)

After adding a replacement rule, if we want to output the/Public string, we only need to add -- PUBLIC -- to the template. the output method of other replacement strings is similar.

V. Summary

Through this article, we have learned about how to define template files, make Template rendering outputs, and assign values to template variables, next we will learn how to use tags in template files to simplify your writing.

PS: We recommend several formatting and beautification tools on this site. I believe you can use them in future development:

Php code online formatting and beautification tools:
Http://tools.bitsCN.com/code/phpformat

JavaScript code beautification/compression/Formatting/encryption tools:
Http://tools.bitsCN.com/code/jscompress

Online XML formatting/compression tools:
Http://tools.bitsCN.com/code/xmlformat

JSON code formatting and beautification tools:
Http://tools.bitsCN.com/code/json

Online XML/JSON conversion tools:
Http://tools.bitsCN.com/code/xmljson

SQL code online formatting and beautification tools:
Http://tools.bitsCN.com/code/sqlcodeformat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.