Thinkphp learning notes template 2

Source: Internet
Author: User
Tags vars smarty template

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 useTmpl_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: TPL/default/user/add.html
The suffix of the template file is. html.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 is assigned a value, you need to call the template file to output the relevant variables. The template call is implemented through the display method. At the end of the operation method, we use:

$ 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 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.

 

4. template replacement

Before template output, the system will perform some special string replacement operations on the rendered template results, that is, the template output is replaced and filtered. 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 __: It will be replaced with the public directory of the current website, which is usually/public/
_ Tmpl __: The template directory to be replaced with the project is usually/project directory/TPL/dfault/
_ 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)
_ 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 areCase SensitiveAnd the replacement rules for 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.

Tmpl_parse_string =>Array(

'_ Public __'=>'/Common',// Change the default _ Public _ replacement rule

'_ Upload __'=>'/Public/uploads /',// Add a new upload path replacement rule

)

 

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,
8. 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, the fetch method mentioned above is called internally to obtain the template output and then 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
9. template engine

The system supports native PHP templates and has built-in an XML-based efficient compilation template engine, which is superior 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 using PHP native in template files.CodeIf you need to use PHP as the template engine, you can configure the following:

'Tmpl _ engine_type'=>'Php'

Optimal efficiency

10. 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:

'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.

 

6. layout templates

<! -- Layout: Template file rule: cache time (seconds) -->
<! -- Layout: public: Header: 60 -->
7. system template

The system has some built-in template files for abnormal pages and pages.TraceYou can customize these template pages to meet your needs. Default System Templates include:

PageTraceTemplate: the default is in the system directoryTPL/pagetrace. TPL. phpIsPHPFile, changeableTmpl_trace_file.

exception template: It is located in the system directory by default. TPL/thinkexception. TPL. php , 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.