Home Backend Development PHP Tutorial 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

Mar 17, 2017 am 09:31 AM
thinkphp template engine

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1659
14
PHP Tutorial
1258
29
C# Tutorial
1232
24
How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

How is the performance of thinkphp? How is the performance of thinkphp? Apr 09, 2024 pm 05:24 PM

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

Development suggestions: How to use the ThinkPHP framework for API development Development suggestions: How to use the ThinkPHP framework for API development Nov 22, 2023 pm 05:18 PM

Development suggestions: How to use the ThinkPHP framework for API development. With the continuous development of the Internet, the importance of API (Application Programming Interface) has become increasingly prominent. API is a bridge for communication between different applications. It can realize data sharing, function calling and other operations, and provides developers with a relatively simple and fast development method. As an excellent PHP development framework, the ThinkPHP framework is efficient, scalable and easy to use.

See all articles