search
HomeBackend DevelopmentPHP TutorialSummary 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 file
return 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:

<&#63;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 ;
&#63;>
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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1111891.htmlTechArticleA 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:...
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
thinkphp是不是国产框架thinkphp是不是国产框架Sep 26, 2022 pm 05:11 PM

thinkphp是国产框架。ThinkPHP是一个快速、兼容而且简单的轻量级国产PHP开发框架,是为了简化企业级应用开发和敏捷WEB应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。

一起聊聊thinkphp6使用think-queue实现普通队列和延迟队列一起聊聊thinkphp6使用think-queue实现普通队列和延迟队列Apr 20, 2022 pm 01:07 PM

本篇文章给大家带来了关于thinkphp的相关知识,其中主要介绍了关于使用think-queue来实现普通队列和延迟队列的相关内容,think-queue是thinkphp官方提供的一个消息队列服务,下面一起来看一下,希望对大家有帮助。

thinkphp的mvc分别指什么thinkphp的mvc分别指什么Jun 21, 2022 am 11:11 AM

thinkphp基于的mvc分别是指:1、m是model的缩写,表示模型,用于数据处理;2、v是view的缩写,表示视图,由View类和模板文件组成;3、c是controller的缩写,表示控制器,用于逻辑处理。mvc设计模式是一种编程思想,是一种将应用程序的逻辑层和表现层进行分离的方法。

实例详解thinkphp6使用jwt认证实例详解thinkphp6使用jwt认证Jun 24, 2022 pm 12:57 PM

本篇文章给大家带来了关于thinkphp的相关知识,其中主要介绍了使用jwt认证的问题,下面一起来看一下,希望对大家有帮助。

thinkphp扩展插件有哪些thinkphp扩展插件有哪些Jun 13, 2022 pm 05:45 PM

thinkphp扩展有:1、think-migration,是一种数据库迁移工具;2、think-orm,是一种ORM类库扩展;3、think-oracle,是一种Oracle驱动扩展;4、think-mongo,一种MongoDb扩展;5、think-soar,一种SQL语句优化扩展;6、porter,一种数据库管理工具;7、tp-jwt-auth,一个jwt身份验证扩展包。

thinkphp 怎么查询库是否存在thinkphp 怎么查询库是否存在Dec 05, 2022 am 09:40 AM

thinkphp查询库是否存在的方法:1、打开相应的tp文件;2、通过“ $isTable=db()->query('SHOW TABLES LIKE '."'".$data['table_name']."'");if($isTable){...}else{...}”方式验证表是否存在即可。

一文教你ThinkPHP使用think-queue实现redis消息队列一文教你ThinkPHP使用think-queue实现redis消息队列Jun 28, 2022 pm 03:33 PM

本篇文章给大家带来了关于ThinkPHP的相关知识,其中主要整理了使用think-queue实现redis消息队列的相关问题,下面一起来看一下,希望对大家有帮助。

thinkphp3.2怎么关闭调试模式thinkphp3.2怎么关闭调试模式Apr 25, 2022 am 10:13 AM

在thinkphp3.2中,可以利用define关闭调试模式,该标签用于变量和常量的定义,将入口文件中定义调试模式设为FALSE即可,语法为“define('APP_DEBUG', false);”;开启调试模式将参数值设置为true即可。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft