How to make the thinkphp template engine achieve the best efficiency method introduction

Source: Internet
Author: User
This article mainly introduced the thinkphp template engine to achieve the best efficiency of the method, combined with the case of the form of a more detailed analysis of the use of thinkphp template engine and the use of the original eco PHP syntax efficiency issues, the need for friends can refer to the following

This paper analyzes the method of making thinkphp's template engine achieve the best efficiency. Share to everyone for your reference, as follows:

By default, the thinkphp framework system uses the template engine as the built-in template engine. The built-in template engine supports mixed use of PHP's original and template tags in template files.

The thinkphp official development document says the performance of this default built-in template engine is efficient, but not optimal. For the template engine to perform optimally, use PHP itself as the template engine .

Using PHP itself as a template engine is really simple, just configure it on the project's profile conf/config.php:

' Tmpl_engine_type ' = ' PHP '

Using PHP itself as a template engine means that you will no longer be able to use template tags for the template engine used by the system by default on the template file, and you can only use the original native PHP code.

Here's an example of how to manipulate PHP code on a template after using PHP itself as the template engine.

Download the Wblog3.1.2_3 blog program and install and install it (you can also build your own project)

First configure the project w3note\conf\config.php file to add a configuration item:

<?phpreturn Array (... ' Tmpl_engine_type ' = ' PHP ', ...);? >

Then the controller \w3note\lib\action\indexaction.class.php and the corresponding template \w3note\tpl\index\index.html code is emptied for different debugging purposes.

Well, the basic work is done, and then the debug record:

1. Use PHP's original ecological code on the template

IndexAction.class.php Controller Code

<?phpclass Indexaction extends Action {public Function index () {  $this->display ()}}

Index.html Template code:


Output:

$title = ' blog '; echo $title;

After replacing "<?php?>" with <php></php> on the template, the result cannot explain the variable, and the <php></php> tag is not supported.

2. Use query statements directly on the template

Controller code with 1, the template code is as follows


Output:

Welcome to use the Wblog blog program

Controller on the side to stay there seems to have done nothing, the template can be written like this, is too flexible!

3. Call the controller-assigned query results on the template

IndexAction.class.php Controller Code

<?php class Indexaction extends Action {public Function index () {  $vo =m (' News ')->find ();  $this->assign (' Vo ', $vo);  $this->display (); }}

Template index.html Code


Output:

Welcome to use the Wblog blog program

This is no different from the template engine used by the system by default.

4, the function of invoking the Project function library on the template

Controller code with 1, the template code is as follows


Output:

af10ef457ed637b91955369297b8e640

The system default template engine is not clumsy (relative) of the label syntax, the function of the call is so simple!

Summary: using PHP itself as a template engine in thinkphp, can make the performance of the template engine to achieve the best efficiency, in the template need to use the original eco-PHP syntax, the wording is more alive, but the system default template engine template label will lose its role.


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.