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

黄舟
Release: 2023-03-06 17:30:01
Original
1320 people have browsed it

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'
Copy after login

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:

Copy after login

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();
 }
}
Copy after login

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>
Copy after login

Output:

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

Put ""After replacing it with , the result is that the variable cannot be interpreted, indicating that the 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>
Copy after login

Output:

欢迎使用WBlog博客程序
Copy after login
Copy after login

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();
 }
}
Copy after login

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>
Copy after login

Output:

欢迎使用WBlog博客程序
Copy after login
Copy after login

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>
Copy after login

Output:

af10ef457ed637b91955369297b8e640
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template