How to make ThinkPHP template engine achieve the best efficiency

Source: Internet
Author: User
This article mainly introduces how to make ThinkPHP template engine achieve the best efficiency, and analyzes in detail the usage of thinkPHP template engine and the efficiency of using the original php syntax in combination with the instance form, for more information about how to make ThinkPHP template engine more efficient, the usage of the thinkPHP template engine and the efficiency of using the original php syntax are analyzed in detail based on the instance form. For more information, see

This article analyzes how to make ThinkPHP's template engine achieve the best efficiency. We will share this with you for your reference. The details are as follows:

By default, the ThinkPHP framework system uses the built-in template engine by default. The built-in template engine supports mixed use of php original code and template labels in template files.

According to ThinkPHP official development documentation,This default built-in template engine is highly efficient, but not optimal. To achieve the optimal efficiency of the template engine, you must use PHP as the template engine..

Using PHP as the template engine is actually very simple. you only need to configure it in the configuration file Conf/config. php of the project:

'TMPL_ENGINE_TYPE' =>'PHP'

Using PHP as the template engine means that you cannot use the template tag of the default template engine on the template file. you can only use the original php code.

The following example demonstrates how to operate the PHP code on the template after using php itself as the template engine.

Download and install the wblog3.1.2 _ 3 blog program (you can also build your own project)

First, configure the W3note \ Conf \ config. php file to add a configuration item:

 'PHP', ...);?>

Then, clear the controller \ W3note \ Lib \ Action \ IndexAction. class. php and the code of the corresponding template \ W3note \ Tpl \ Index \ index.html for different debugging purposes.

Now that the basic work is complete, the debugging record is as follows:

1. use the original php code on the template

IndexAction. class. php controller code

 display(); }}

Index.html template code:

 Use original php code 
 

Output:

$ Title = 'wangzhi blog'; echo $ title;

In the template" "Replace The result does not support variable interpretation. Label.

2. directly use the query statement on the template

The controller code is the same as 1. the template code is as follows:

 Use original php code 
 Find (); echo $ vo ['title'];?>

Output:

Welcome to WBlog

It seems that the controller has nothing to do while staying. The template can be written in this way, which is too flexible!

3. call the query results allocated by the controller on the template.

IndexAction. class. php controller code

 find();  $this->assign('vo', $vo);  $this->display(); }}

Template index.html code

 Use original php code 
 

Output:

Welcome to WBlog

This is similar to the template engine used by the system by default.

4. call the function of the project function library on the template

The controller code is the same as 1. the template code is as follows:

 Use original php code 
 

Output:

af10ef457ed637b91955369297b8e640

Instead of the tag syntax of the system default template engine (relatively speaking), function calls are so simple!

Summary:Using PHP as the template engine in ThinkPHP can optimize the performance of the template engine. in the template, you need to use the original php syntax, which is quite easy to write, however, the template tag of the default template engine is ineffective.


The above describes how to make ThinkPHP template engine the best efficiency. For more information, see other related articles in the first PHP community!

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.