ThinkPHP learning notes Template 2

Source: Internet
Author: User
1. template definition default template file definition rules are: Template directory/template topic/[group name/] module name/operation name + template suffix Template directory is the Tpl under the project by default, the default template topic is d...

1. Template definition

The default template file definition rules are as follows:

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

The default template directory is Tpl under the project, and the default template topic is default. the template topic function is designed for switching between multiple templates. if there are multiple template topics, you can use the TMPL_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/default/User/add.html

The suffix of the template file is .html. you can also configure it as another one through TMPL_TEMPLATE_SUFFIX.

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/default/Home/User/add.html.

2. Template assignment

To output variables in the template, you must pass the variables to the template in the Action Class. The View class provides the assign method to assign values to the template variables. all variable types use The assign method to assign values.

$ This-> assign ('name', $ value );

3. template call

After the template variable value is assigned, you need to call the template file to output the relevant variables. the template call is implemented through the display method. We use the following method at the end of the operation:

$ This-> display ();

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

1. call other operation templates of the current module

Format: display ('Operation name ')

For example, if the current operation is the read operation under the User module, we need to call the edit operation template of the User module and use:

$ This-> display ('Edit ');

You do not need to write the path and suffix of the template file.

2. call the operation templates of other modules

Format: display ('group name: module name: Operation name'). The group name is optional.

For example, if the User module is used, call the read operation template of the Member module and use:

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

3. call the operation templates of other themes

Format: display ('topic name @ module name: Operation name ')

For example, we need to call the edit operation template of the User module of the Xp topic, and use:

$ This-> display ('XP @ User: edit ');

IV. direct full-path output Template

Format: display ('Template filename ')

For example, we directly output the menu.html template file under the current publicdirectory, using:

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

In this way, you need to specify the template path and suffix. The Public directory is located under the file location of the current project Portal. if it is another suffix file, you can also directly output

4. Template replacement

Before the template is output, the system will 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/default/Public/

_ PUBLIC __: The Public directory of the current website is usually/PUBLIC/

_ TMPL __: The Template directory that will be replaced with the project is usually/project directory/Tpl/dfault/

_ ROOT __: 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)

_ URL __: it will be replaced with the URL address of the current module (excluding the domain name)

_ 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 will be changed:

  1. TMPL_PARSE_STRING => array (
  2. '_ PUBLIC _' => '/common', // change the default _ PUBLIC _ replacement rule
  3. '_ UPLOAD _' => '/Public/Uploads/', // add a new UPLOAD path replacement rule.
  4. )

5. get content

Sometimes we do not want to directly output the template content, but want to output the content after some processing. we can use the fetch method to obtain the parsed template content, which is used in the Action class:

$ Content = $ this-> fetch ();

The parameter usage of fetch is basically the same as that of the Display method,

6. static generation

ThinkPHP provides a flexible static file generation function, which can generate the required static file while outputting the template for calling.

You can use the buildHtml method in the Action to create a static file. The first parameter of the buildHtml method requires the generated static file name. The following parameters are consistent with the display method, internally, we call the fetch method mentioned above to obtain the template output and create a static file. the usage is as follows:

$ This-> buildHtml ('static file', 'static path', 'Template file ');

If the static path is left blank, it is stored in the Html directory defined by HTML_PATH (the default HTML_PATH is located in the Html directory under the Project Directory, if not, manually created) by default. static files can be set at will, the path can also be included. if the path does not exist, the system will automatically create

7. Template engine

The system supports native PHP templates and has built-in an XML-based efficient compilation template engine, which is superior to Smarty in terms of functionality and performance, the default template engine used by the system is the built-in template engine. for details about how to use the template engine labels, see the template guide.

The built-in template engine can also directly support mixed use of PHP native code and template labels in the template file. if you need to completely use PHP as the template engine, you can configure:

'Tmpl _ ENGINE_TYPE '=> 'php'

Optimal Efficiency

8. use a third-party template engine

The system supports the template engine extension mechanism and officially provides third-party template engine extensions including Smarty, EaseTemplate, TemplateLite, and Smart. The following uses the Smarty template engine as an example to describe how to use a third-party template engine.

First, you need to download the official template engine extension and put it under the LibThinkUtilTemplate directory in the system directory. then, download the latest Smarty template engine file and put it in the Vendor third-party class library directory in the system directory.

The rest is to simply configure the template engine name. for example, in the project configuration file, set 'tmpl _ ENGINE_TYPE '=> 'smarty'

The difference is that the display method directly outputs the rendered content of the template file, while the fetch method returns the rendered content of the template file. The developer determines how to process the returned content. This is another advanced method for template replacement, which is flexible and does not need to be configured.

Note that the fetch method still performs the above template replacement operation.

9. Layout Template

10. System Template

The system has some built-in template files for output of abnormal pages and page Trace functions. you can customize these template pages to meet your own needs. Default system templates include:

Page Trace Template: By default, Tpl/PageTrace. tpl. php in the system directory is a php file. you can change TMPL_TRACE_FILE for configuration.

Exception template:Tpl/ThinkException. tpl. php in the system directory by default. you can change TMPL_EXCEPTION_FILE for configuration.

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.