ThinkPHP3.1 quick start (8) view

Source: Internet
Author: User
After learning about the controller and model operations, 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. After learning about the controller and model operations, 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.

To manage template files more effectively, ThinkPHP divides the directory of template files. 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 DEFAULT_THEMESet the default template topic name for the parameter.
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:
  1. Tpl/User/add.html
The suffix of the Copy code template file is. html. you can also use TMPL_TEMPLATE_SUFFIX. For example, we can configure:
  1. 'Tmpl _ TEMPLATE_SUFFIX '=>'. tpl'
After the code definition is copied, the template file corresponding to the add operation of the User module becomes:
  1. Tpl/User/add. tpl
Copy the code. if the project enables the module grouping function (assuming that the User module belongs to the Home group), the default template file may be:
  1. Tpl/Home/User/add.html
In the copy code grouping mode, if you think the directory structure is too deep, you can set TMPL_FILE_DEPRParameters to simplify the Template directory level, such as setting:
  1. 'Tmpl _ file_depr' => '_'
Copy the default template file of the code to the following:
  1. Tpl/Home/User_add.html
Because the system has such a rule to automatically recognize template files, copying code simplifies the rendering and output of templates. [-More-]

After the template rendering 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'])
Type 2: display ('complete template filename '[, 'character encoding'] [, 'output type'])

The following is a typical usage without any parameters:
  1. $ This-> display ();
Copying the code indicates that 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 method 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:
  1. $ This-> display ('Edit ');
Copy the code to call the edit template under the current module.
  1. $ This-> display ('Member: read ');
Copy the code to call the read template under the Member module.
If the template topic function is used, cross-topic calling is also supported. use:
  1. $ This-> display ('theme: User: edit ');
Copy the code to call 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.
  1. $ This-> display ('public: menu ');
Copy the code to 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:
You can also specify the encoding and type:
  1. $ This-> display ('read', 'utf-8', 'text/XML ');
Copy the code to output the 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:
  1. $ This-> display ('./Public/menu.html ');
To copy the code, 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:
  1. $ This-> display ('./Public/menu. tpl ');
Copy the code 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:
  1. $ Content = $ this-> fetch ('Member: edit ');
Copy the code and use the fetch method to obtain the rendering content. you can perform operations such as filtering and replacement to meet 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,
  1. $ This-> show ($ content );
You can also specify the encoding and type for the code to be copied:
  1. $ This-> show ($ content, 'utf-8', 'text/XML ');
You can also copy the content in the show method to parse the template.

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.
  1. $ This-> assign ('name', $ value );
  2. // The following statement is equivalent.
  3. $ This-> name = $ value;
The assign method of the Copy code 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:
  1. {$ Name}
Copy the code to output multiple Template variables at the same time, you can use the following method:
  1. $ Array ['name'] = 'thinkphp ';
  2. $ Array ['email '] = 'liu21st @ gmail.com ';
  3. $ Array ['phone'] = '000000 ';
  4. $ This-> assign ($ array );
Copy the code to output the name, email, and phone variables in the template file.
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:
If you use the built-in template engine to copy code, you can use:
  1. {$ Name} [{$ email} {$ phone}]
Copy the code to output the same content.
For more information about the use of template tags, we will explain in detail in the following template tags.

Before the template is output, the system can also 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:
../Public: It will be replaced with the Public Template directory of the current project, which is usually/project directory/Tpl/current topic/Public/
_ TMPL __: The Template directory to be replaced with the project is usually/project directory/Tpl/current topic/
(Note: For deployment security considerations,.../Public and _ TMPL _ are no longer recommended)
_ PUBLIC __: It will be replaced with the Public directory of the current website, which is usually/Public/
_ ROOT __: It will be replaced with the address of the current website (excluding the domain name)
_ APP __: It will be replaced with the URL address of the current project (excluding the domain name)
_ GROUP __: Will be replaced with the URL address of the current group (excluding domain names)
_ URL __: Will be replaced with the URL address of the current module (excluding domain names)
_ ACTION __: It will be replaced with the URL of the current operation (excluding the domain name)
_ SELF __: 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:
  1. 'Tmpl _ PARSE_STRING '=> array (
  2. '_ PUBLIC _' => '/common', // change the default _ PUBLIC _ replacement rule
  3. '_ JS _' => '/Public/JS/', // add a new JS class library path replacement rule
  4. '_ UPLOAD _' => '/Uploads', // add a new UPLOAD path replacement rule
  5. )
After the Copy code has a template replacement rule, all the _ PUBLIC _ strings on the page will be replaced. if The _ PUBLIC _ string needs to be output to the template, we can add replacement rules, for example:
  1. 'Tmpl _ PARSE_STRING '=> array (
  2. '-- PUBLIC --' => '_ PUBLIC _', // use the new rule to output the _ PUBLIC _ string.
  3. )
After you copy the code to add a replacement rule, if you want to output the _ PUBLIC _ string, you only need to add -- PUBLIC -- to the template. the other string replacement methods are similar.

In summary, we learned how to define template files, render templates, and assign values to template variables, next we will learn how to use tags in template files to simplify your writing.

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.