Home  >  Article  >  Backend Development  >  An introduction to how to make ThinkPHP's template engine achieve optimal efficiency

An introduction to how to make ThinkPHP's template engine achieve optimal efficiency

黄舟
黄舟Original
2017-03-17 09:31:251335browse

This article mainly introduces how to achieve the best efficiency of ThinkPHP's template engine. It analyzes in detail the use of thinkPHP's template engine and the use of original php syntax in the form of examples. Efficiency issues, friends in need can refer to

This article analyzes the method to achieve the best efficiency of ThinkPHP's template engine. Share it with everyone for your reference, the details are as follows:

By default ThinkPHP FrameworkThe template engine used by the system by default is the built-in template engine. The built-in template engine supports the mixed use of PHP original code and template tag in template files.

ThinkPHP official development documentation says that The performance of this default built-in template engine is efficient, but it is not optimal. To achieve optimal performance of the template engine, it is necessary to use PHP itself as the template engine.

Using PHP itself as a template engine is actually very simple, just configure it in the project's configuration file Conf/config.php:

'TMPL_ENGINE_TYPE' =>'PHP'

Use PHP itself as a template engine Finally, it means that you will no longer be able to use the template tag of the template engine used by the system by default on the template file. You can only use the original PHP code.

The following will demonstrate through examples how to operate PHP code on the template after using PHP itself as the template engine.

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

First configure the project W3note\Conf\config.php file and add a configuration item:

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

Okay, the basic work has been done. Next is the debugging record:

1. Use PHP original ecological code on the template

IndexAction.class.phpController code

<?php
class IndexAction extends Action {
 public function index(){
  $this->display();
 }
}

index.html Template code:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>使用原生态的php代码</title>
</head>
<body>
 <?php
 $title= &#39;网志博客&#39;;
 echo $title;//输出变量
 ?>
</body>
</html>

Output:

$title=&#39;网志博客&#39;; echo $title;

Put "2b2ce2448fc0812017699c4241f04162"After replacing it with 453490439c74a929a2834bf146aa58a45c6c8f72abb80ea0f248b656fc7e404b, the result is that the variable cannot be interpreted, indicating that the 453490439c74a929a2834bf146aa58a45c6c8f72abb80ea0f248b656fc7e404b tag is not supported.

2. Use querystatement directly on the template

The controller code is the same as 1, and the template code is as follows

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>使用原生态的php代码</title>
</head>
<body>
 <?php
 $vo=M(&#39;News&#39;)->find();
 echo $vo[&#39;title&#39;];
 ?>
</body>
</html>

Output:

欢迎使用WBlog博客程序

The controller seems to be doing nothing while sitting aside. It can be written like this on the template. It is so flexible!

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

IndexAction.class.php controller code

<?php class IndexAction extends Action {
 public function index(){
  $vo=M(&#39;News&#39;)->find();
  $this->assign(&#39;vo&#39;, $vo);
  $this->display();
 }
}

Template index.html code

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>使用原生态的php代码</title>
</head>
<body>
 <?php
 echo $vo[&#39;title&#39;];
 ?>
</body>
</html>

Output:

欢迎使用WBlog博客程序

This situation is no different from the way the system uses the template engine by default.

4. Call the project function on the template Library function

The controller code is the same as 1, and the template code is as follows

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>使用原生态的php代码</title>
</head>
<body>
 <?php
 echo pwdHash(&#39;abc&#39;);//调用项目\W3note\Common\common.php函数库的加密函数pwdHash()
 ?>
</body>
</html>

Output:

af10ef457ed637b91955369297b8e640

Abandoned the clumsy (relatively speaking) tag syntax of the system default template engine, the function call is so simple!

Summary: Using PHP itself as the template engine in ThinkPHP can maximize the performance of the template engine. You need to use the original PHP syntax and writing method on the template. It is relatively lively, but the template tag of the system default template engine will lose its function.


The above is the detailed content of An introduction to how to make ThinkPHP's template engine achieve optimal efficiency. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn