How to make ThinkPHP template engine optimal efficiency

Source: Internet
Author: User
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. 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 documents, the performance of this default built-in template engine is efficient, but it is not the best. 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:
  1. Return array (
  2. ...
  3. 'Tmpl _ ENGINE_TYPE '=> 'php ',
  4. ...
  5. );
  6. ?>
Copy the code and 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
  1. Class IndexAction extends Action {
  2. Public function index (){
  3. $ This-> display ();
  4. }
  5. }
Copy code index.html template code:
  1. Use original php code
  2. $ Title = 'wangzhi blog ';
  3. Echo $ title; // output variable
  4. ?>
Copy code output: Wangzhi blog

Like the original php code, variables can be declared on the template and variables can be output, and code comments can be identified. However, you must use the php start flag" No, why? Next you will know.


2. the controller code is the same as above. the template code is as follows:
  1.  
  2. Use original php code
  3. $ Title = 'wangzhi blog ';
  4. Echo $ title;
Copy the code output: $ title = 'blog blog '; echo $ title;

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

3. directly use the query statement on the template
The controller code is the same as 1. the template code is as follows:
  1. Use original php code
  2. $ Vo = M ('news')-> find ();
  3. Echo $ vo ['title'];
  4. ?>
Copy code 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!

4. call the query results allocated by the controller on the template.
IndexAction. class. php controller code
  1. Class IndexAction extends Action {
  2. Public function index (){
  3. $ Vo = M ('news')-> find ();
  4.  
  5. $ This-> assign ('vo', $ vo );
  6.  
  7. $ This-> display ();
  8. }
  9. }
Copy code template index.html code
  1. Use original php code
  2. Echo $ vo ['title'];
  3. ?>
Copy code output: Welcome to WBlog

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

5. 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:
  1. Use original php code
  2. Echo pwdHash ('ABC'); // call the encryption function pwdHash () of the project \ W3note \ Common \ common. php function library ()
  3. ?>
Copy the code output: af10ef457ed637b9195541797b8e640

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


Conclusion: 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, however, the template tag of the default template engine is ineffective.
Address: http://w3note.com/web/106.html

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.