The display method in thinkphp3.x and the usages of Show method _php example

Source: Internet
Author: User
Tags php template smarty template

This article illustrates the display method in thinkphp3.x and the use of Show method. Share to everyone for your reference, specific as follows:

After understanding the controller and model operation in the previous article, we begin to familiarize ourselves with the View section, the view in thinkphp mainly refers to the template file and the template engine, this article first understands the template file and how to render the output.

One, template definition

To manage template files more effectively, thinkphp the template files, and 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 the grouping is defined, the subdirectory is separated by the grouping name, and the new template theme is empty (meaning that the template theme feature is not enabled).

Template theme features are designed for multiple template switching, and if you have multiple template themes, you can set the default template theme name with the Default_theme parameter.

Under each template topic, the project's module name is the directory, followed by the concrete action template file for each module, for example:

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

Tpl/user/add.html

The default suffix for the template file is. html, or it can be configured with Tmpl_template_suffix. For example, we can configure:

' Tmpl_template_suffix ' => '. TPL '

Once defined, the template file for the add operation of the user module becomes:

Tpl/user/add.tpl

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

Tpl/home/user/add.html

Under 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, such as setting:

' Tmpl_file_depr ' => ' _ '

The default template file becomes:

Tpl/home/user_add.html

It is because the system has such a template file automatic recognition rules, so to our template rendering output simplified.

Second, template rendering

After the template is defined, the output can be rendered using the display and show methods. The display method requires that we have a template file defined, while the Show method renders the content output directly.

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

First type:

Display (' [Subject:] [module:] [operation] ' [, ' character encoding '] [, ' Output type ']]

The second type:

Display (' Full template filename ' [, ' character encoding '] [, ' Output type '])

The following is one of the most typical uses, with no parameters:

$this->display ();

Indicates that the system will automatically locate the template file according to the default rules, so it is common for the display method to output the corresponding template without taking any parameters, which is the simplest usage of the template output.

If you do not define the template file according to the template definition rules, or if I need to invoke a template below another module, you can use:

$this->display (' edit '); 

Indicates that the edit template under the current module is invoked

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

Represents the read template below the call member module.

If we use the template theme feature, we can also support cross theme calls, using:

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

Represents an edit template that invokes the user module under the theme topic.

This way of rendering output does not need to write template file path and suffix, exactly, the module and operation does not necessarily need to have a corresponding module or operation, just a directory name and file name, for example, your project may not have the public module, There is no menu action for the public module, but you can use the

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

Output this template file. Understanding this, the template output is clear.

The display method supports specifying output encodings and types when rendering the output, for example:

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

Represents the Output XML page type (you can output many types in conjunction with your application requirements).

There is always a special case, if the template directory is customized, or do not need to be in the module for the directory storage, then the default display rendering rules can not be processed, this time, we need to use a different way to deal with directly in the template file name, such as:

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

This way you need to specify the template path and suffix, where the public directory is located under the current project portal file location. In the case of other suffix files, direct output is also supported, for example:

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

As long as./PUBLIC/MENU.TPL is a template file that actually exists.

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

In another case, you need to get the output of the render template, you can use the Fetch method, the use of the Fetch method and display is basically consistent, the difference is that the fetch method is not directly output after rendering, but instead returns the rendered content, such as:

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

After you use the Fetch method to get the rendered content, you can filter and replace the complex requirements for the template output.

If you do not define any template files, or store the template contents in a database, you need to use the Show method to render the output, the calling format of the presentation method:

Show (' Render 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.

Third, the template assignment

We know how to render the template output, but if you want to output the variable in the template, you must pass the variable to the template in the controller, providing the Assign method to assign a value to the template variable, regardless of which variable type is uniformly using the Assign assignment.

$this->assign (' name ', $value);
The following wording is equivalent:
//$this->name = $value;

The Assign method must be called before the display and show methods, and the system will only output the set variable, and the other variables will not be output (System variables can be exported through special tags, can not be assigned to the template variable), to some extent to ensure the security of the variable.

When you assign a value, you can output the variable in the template file, and if you are using a built-in template, you can output it like this:

{$name}

If you want to output multiple template variables at the same time, you can use the following methods:

$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 three variables in the template file at the same time.

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

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

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

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

Output the same content.

For more template tags, we'll explain them in detail in the template tags that follow.

Four, template replacement

Before the output of the template, the system can also make some template for the rendering of the special string replacement operations, that is, to achieve the template output replacement and filtering. This mechanism makes the definition of template files more convenient, and the default replacement rules are:

.. /public: The common template directory that will be replaced with the current project is usually the/project directory/tpl/the current theme/public/

__tmpl__: The template directory that will be replaced with the project is usually the/project directory/tpl/the current theme/

(Note: In order to deploy security considerations,.. /public and __tmpl__ are no longer recommended for use)

__PUBLIC__: The public directory that will be replaced with the current Web site is usually/public/

__ROOT__: Replace the address of the current Web site (excluding the domain name)

__APP__: Replaces the URL address of the current item (excluding the domain name)

__GROUP__: Replaces the URL address of the current group (excluding the domain name)

__URL__: Replace the URL address of the current module (excluding the domain name)

__ACTION__: Replaces the URL address of the current operation (excluding the domain name)

__SELF__: Replaces the current page URL

Note that these special strings are strictly case-sensitive, and that the substitution rules for these special strings can be changed or added, and we only need to configure tmpl_parse_string in the project configuration file to do so. If you have the same array index, the default rules for the system are changed. For example:

' Tmpl_parse_string ' =>array ( 
  ' __public__ ' => '/common ',//change the default/public replacement rule 
  ' __js__ ' => '/public/ js/',//Add new JS class library path replacement rule 
  '/uploads ' => '/uploads ',//Add new upload path replacement rule 


With the template replacement rule, all the __public__ strings on the page are replaced, so if you really need to output the __public__ string to the template, we can add the substitution rule by adding a way, for example:

' Tmpl_parse_string ' =>array ( 
  '--public--' => ' __public__ ',//output/public string with new rule 
)

With this addition to the replacement rule, if we want to output the __public__ string, we simply add--public--to the template, and the other replacement strings are exported in a similar way.

V. Summary

Through this study, we have probably mastered how to define template files and make template rendering output, and how to assign value template variables, and later we will learn how to use tags in template files to simplify your writing.

PS: Here recommend a few of the format of this site landscaping tools, I believe that we can use in future development:

PHP code online format Landscaping tools:
Http://tools.jb51.net/code/phpformat

JavaScript code Landscaping/compression/formatting/encryption Tools:
http://tools.jb51.net/code/jscompress

Online XML format/compression tools:
Http://tools.jb51.net/code/xmlformat

JSON Code Formatting Landscaping tool:
Http://tools.jb51.net/code/json

Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson

SQL code Online formatting Landscaping tools:
Http://tools.jb51.net/code/sqlcodeformat

More interested in thinkphp related content readers can view the site topics: "thinkphp Introductory Course", "thinkphp Common Methods Summary", "Smarty Template Primer" and "PHP template technology Summary."

I hope this article will help you with the PHP program design based on thinkphp framework.

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.