


Summary of how ThinkPHP uses Smarty third-party plug-ins, thinkphpsmarty_PHP tutorial
A summary of how ThinkPHP uses Smarty third-party plug-ins, thinkphpsmarty
This article describes how ThinkPHP uses Smarty third-party plug-ins. Share it with everyone for your reference, the details are as follows:
If you don’t want to use TP’s own template system when using the ThinkPHP framework, but use a third-party template system, you have many other options. Here I will only introduce Smarty, which is more official and powerful. Template system.
Since Smarty is compatible with PHP4, its efficiency will be relatively low. This low is only relative. It is estimated that when Smarty officially abandons PHP4, the efficiency may rise to a big level.
Under the PlugIns directory of the TP framework, there is a SmartTemplate directory, which contains the Smarty plug-in that comes with the system.
How to use it:
1. Add:
to the project’s Conf/Config.php filereturn array( 'THINK_PLUGIN_ON' => true, 'TMPL_ENGINE_TYPE'=>'smarty', );
2. Download Smarty and copy the entire libs directory of smarty to the PlugIns directory of the project (note that the PlugIns directory may not exist and you need to create it yourself). At the same time, rename the libs directory to SmartTemplate (I hope you didn’t remember it) Wrong, in fact, it has the same name as the SmartyTemplate directory in THINKPHP's PlugIns directory). If you are unwilling to change the directory to this name, then you must go to the TP's plug-in directory to modify the plug-in file to make the include path correct.
3. Pay attention to modifying and deleting the html file under Temp after each modification of the action or template file
Explanation: The above content comes from the official website and was answered by friend lin_chaoqi. The URL is: http://bbs.thinkphp.cn/viewthread.php?tid=305&highlight=smarty
The method I want to mention here is different from the above, Heihei
Because when I was using a third-party template plug-in, I specifically looked at TP's view.class.php and discovered some very important issues, that is, if a third-party template plug-in is used, the efficiency of the third-party template plug-in may not be guaranteed. Because the fetch method of the View class does a lot of its own processing for the TP template plug-in when determining whether it is a third-party plug-in, and these are almost completely ineffective when using third-party template plug-ins, these processing may cause problems for the third-party template plug-in. The impact of third-party plug-ins also affects the execution efficiency of third-party plug-ins. The problem has been communicated with Liannian, but since the changes may be huge, perhaps Liannian will not try to make improvements in the recent versions. Firstly, it is afraid of affecting those programs that already use third-party plug-ins. Secondly, if it is removed These processing, then the View class may not be needed. Liu Nian should be unwilling to see such a situation. After all, this has also affected the architecture of the original system, and I guess I have to think about it carefully... [Of course, from a personal perspective, I definitely hope that everyone will use TP's own template plug-ins, but I am more familiar with smarty at the moment. [self], but for me as a user, what I need is a temporary solution, so here is the following content.
In order to solve this problem, I can only start from View.class.php, because there is a line in Action.class.php:
$this->tpl = View::getInstance();
So, that is to say, the tpl variable is the singleton mode of View. I checked the getInstance method in View.class.php and found that the get_instance_of function is used in it (this function has a small bug, here No explanation, but I don’t have a better solution at the moment), so I changed the getInstance and __construct methods, deleted the __construct method, and added the init method. The modified code is as follows:
static function getInstance() { get_instance_of(__CLASS__,'init'); init ($type=''){ $type)) { $this->type = strToUpper( $type ); $this->type = strtoupper(C('TMPL_ENGINE_TYPE')); in_array( $this->type, array('PHP','THINK') ) ){ $type = ucfirst( strToLower( $this->type ) ); vendor( $type ); $type(); $this; return } public function if(!empty( }else{ } if ( ! return new } return }
That is, the View class calls the init method at the same time when it is instantiated. In this method, I put my own template plug-in in the third-party plug-in directory (Vendor).
Remember: Never miss the last sentence return $this;. In fact, this is what I call the bug of get_instance_of. If you do not add this sentence, then when the type variable is PHP or THINK, getInstance cannot return the instance. of.
The new usage steps are as follows:
1. Modify the project’s Conf/Config.php file:
return array( 'THINK_PLUGIN_ON' => true, 'TMPL_ENGINE_TYPE'=>'TpSmarty', );
2. Create TpSmarty.php under the Vendor directory of TP with the following content:
<?php include_once(PLUGIN_PATH."smarty/Smarty.class.php"); TpSmarty extends Smarty { __construct (){ parent::Smarty(); $this->caching = true; $this->template_dir = TMPL_PATH; $this->compile_dir = CACHE_PATH ; $this->cache_dir = TEMP_PATH ; ?> class public function } }
The above is the simplest way to write it. In actual use, please change these variables to match your own site.
3. According to the include_once function in the above file, copy the libs directory of smarty to the PlugIns directory of the project and rename it: smarty (it only needs to match the directory in include_once)
4. Then, you can use it directly in the project method:
class IndexAction extends Action{ index(){ $this->assign('test','testss'); $this->display('default/index.html'); public function } } }
It’s just that after using the plug-in, the parameter of the display method is the full path of the template and cannot be left blank (it’s not that it can’t be solved, but more code needs to be changed. Currently, this method has the least changes).
Test it, is it normal? hehe .
Now, we change the template engine in Config back to Think, and at the same time create an Index directory under the Tpl/default/ directory, put index.html in it, and modify the index() method above to replace the original $this- >display('default/index.html'); Change to $this->display(); Try it, is it normal?
Readers who are interested in more thinkPHP-related content can check out the special topics on this site: "ThinkPHP Getting Started Tutorial", "ThinkPHP Common Methods Summary", "Smarty Template Basic Tutorial" and "PHP Template Technology Summary".
I hope this article will be helpful to everyone’s PHP programming based on the ThinkPHP framework.
Articles you may be interested in:
- Thinkphp smarty uploadify implements refresh-free upload
- How ThinkPHP uses smarty template engine
- Solution to using upload under ThinkPHP How to use the plug-in Uploadify browser to report a 302 error in firefox
- ThinkPHP3.2.2 plug-in controller function
- How to use the Duoshuo comment plug-in on ThinkPHP
- ThinkPHP3.2.2 plug-in Brief description of controller functions
- Summary of thinkPHP query methods
- Detailed explanation of thinkphp namespace usage examples
- Analysis of widget extension usage examples under thinkPHP
- thinkphp realizes unlimited Classification (using recursion)
- Thinkphp implements automatic verification and auto-completion

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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.

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.

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.

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.

"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.

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.

RPC service based on ThinkPHP6 and Swoole implements file transfer function Introduction: With the development of the Internet, file transfer has become more and more important in our daily work. In order to improve the efficiency and security of file transfer, this article will introduce the specific implementation method of the RPC service based on ThinkPHP6 and Swoole to implement the file transfer function. We will use ThinkPHP6 as the web framework and utilize Swoole's RPC function to achieve cross-server file transfer. 1. Environmental standard
