ThinkPHP使用Smarty第三方插件方法小结
本文实例讲述了ThinkPHP使用Smarty第三方插件的方法。分享给大家供大家参考,具体如下: 如果你在使用ThinkPHP框架的时候不想采用TP自带的模版系统,而使用第三方的模版系统,你有很多其他的选择,在这里我仅介绍Smarty这种比较官方,而且比较强大的模版系统
本文实例讲述了ThinkPHP使用Smarty第三方插件的方法。分享给大家供大家参考,具体如下:
如果你在使用ThinkPHP框架的时候不想采用TP自带的模版系统,而使用第三方的模版系统,你有很多其他的选择,在这里我仅介绍Smarty这种比较官方,而且比较强大的模版系统。
由于Smarty兼容PHP4,因此,它的效率会相对低一点点,这个低只是相对的,估计等Smarty啥时候正式放弃PHP4的时候,效率可能会上很大一个台阶。
在TP框架的PlugIns目录下面,有一个SmartTemplate目录,里面就是系统自带的Smarty插件。
使用方法如下:
1、在项目的Conf/Config.php文件里加上:
return array( 'THINK_PLUGIN_ON' => true, 'TMPL_ENGINE_TYPE'=>'smarty', );
2、下载好Smarty,并将smarty的libs目录整个拷至项目的PlugIns目录下(说明一下,PlugIns目录可能会不存在,需要自己建立),同时将libs目录改名为SmartTemplate(希望没有记错,其实也就是与THINKPHP的PlugIns目录里的SmartyTemplate目录同名即可),如果你不愿意把目录改成这个名字,那么,你必须到TP的插件目录里修改插件文件,使之包含路径正确。
3、注意修改每次修改action或模板文件后删除Temp下的html文件
说明一下:上面的内容来自于官方,由lin_chaoqi朋友解答的,网址为:http://bbs.thinkphp.cn/viewthread.php?tid=305&highlight=smarty
在这里我要提的方法是于上面不一样的,黑黑
因为我在使用第三方模版插件的时候,特地看了TP的view.class.php发现了一些很重要的问题,那就是,如果采用第三方模版插件,那么第三方模版插件的效率可能不能保证,因为View类的fetch方法在判断是否为第三方插件之间,作了很多TP模版插件的自有处理,而这些对于使用第三方模版插件来说,几乎是完全无效的,这些处理可能会给第三方插件带来影响,同时也影响了第三方插件的执行效率。问题已经与流年沟通过,但由于改动可能会很大,或许最近几个版本里,流年都不会尝试作改进吧,一是怕影响了那些已经使用第三方插件的程序,二来如果去除掉这些处理,那么View类可能就不需要了。流年对于这样的情况应该是不愿意看到的。毕竟这也影响了原有系统的架构,估计流年得仔细考虑了……[当然从流年个人来说,肯定是希望大家都使用TP自有的模版插件,只是我目前对于smarty则是更加熟悉而己],但对于我这个使用者来说,我需要的是临时解决方法,所以,就有了下面的内容。
为了解决这个问题,我只有从View.class.php里下手,因为Action.class.php里有一行:
$this->tpl = View::getInstance();
那么,也就是说 tpl 这个变量是 View 的单例模式,检查了一下View.class.php中的这个getInstance方法,发现里面使用了 get_instance_of 这个函数(这个函数是有一点小BUG的,这里不作解释,但我目前也没有更好的解决方法),于是我对getInstance和__construct两个方法进行了改动,删除了__construct方法,加上了init方法,改动代码如下:
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 }
也就是让View类在实例化的时候,同时调用init方法。在这个方法里,我将我自己的模版插件放到了第三方插件的目录(Vendor)下。
切记切记:千万不可漏掉最后一句return $this;,其实这就是我所说的get_instance_of的BUG,如果不加这句,那么当type变量为PHP或THINK时,getInstance是无法返回实例的。
新的使用方法步骤如下:
1、修改项目的Conf/Config.php文件:
return array( 'THINK_PLUGIN_ON' => true, 'TMPL_ENGINE_TYPE'=>'TpSmarty', );
2、在TP的Vendor目录下面,创建TpSmarty.php,内容如下:
<?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 } }
上面是最简单的写法,实际使用中,这些变量请改为与你自己的站点相配合。
3、根据上面的文件里的include_once函数,将smarty的libs目录拷贝至项目的PlugIns目录下,改名为:smarty(只需要与include_once中的目录相匹配即可)
4、然后,在项目的方法里就可以直接使用:
class IndexAction extends Action{ index(){ $this->assign('test','testss'); $this->display('default/index.html'); public function } } }
只是,使用了插件后,display方法的参数是模版的全路径,而且不能留空(并非不能解决,只是要改动的代码就会更多,目前这个方法是改动最少的)。
测试一下,是否正常了?呵呵 。
现在,我们把Config里的模版引擎换回Think,同时在Tpl/default/目录下建立Index目录,里面放上index.html,并修改上面的index()方法,将原来的$this->display('default/index.html'); 改为$this->display(); ,试一下,是不是也正常了?
更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《smarty模板入门基础教程》及《PHP模板技术总结》。
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

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

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

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

Unfortunately, people often delete certain contacts accidentally for some reasons. WeChat is a widely used social software. To help users solve this problem, this article will introduce how to retrieve deleted contacts in a simple way. 1. Understand the WeChat contact deletion mechanism. This provides us with the possibility to retrieve deleted contacts. The contact deletion mechanism in WeChat removes them from the address book, but does not delete them completely. 2. Use WeChat’s built-in “Contact Book Recovery” function. WeChat provides “Contact Book Recovery” to save time and energy. Users can quickly retrieve previously deleted contacts through this function. 3. Enter the WeChat settings page and click the lower right corner, open the WeChat application "Me" and click the settings icon in the upper right corner to enter the settings page.

Tomato Novel is a very popular novel reading software. We often have new novels and comics to read in Tomato Novel. Every novel and comic is very interesting. Many friends also want to write novels. Earn pocket money and edit the content of the novel you want to write into text. So how do we write the novel in it? My friends don’t know, so let’s go to this site together. Let’s take some time to look at an introduction to how to write a novel. Share the Tomato novel tutorial on how to write a novel. 1. First open the Tomato free novel app on your mobile phone and click on Personal Center - Writer Center. 2. Jump to the Tomato Writer Assistant page - click on Create a new book at the end of the novel.

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.

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

Mobile games have become an integral part of people's lives with the development of technology. It has attracted the attention of many players with its cute dragon egg image and interesting hatching process, and one of the games that has attracted much attention is the mobile version of Dragon Egg. To help players better cultivate and grow their own dragons in the game, this article will introduce to you how to hatch dragon eggs in the mobile version. 1. Choose the appropriate type of dragon egg. Players need to carefully choose the type of dragon egg that they like and suit themselves, based on the different types of dragon egg attributes and abilities provided in the game. 2. Upgrade the level of the incubation machine. Players need to improve the level of the incubation machine by completing tasks and collecting props. The level of the incubation machine determines the hatching speed and hatching success rate. 3. Collect the resources required for hatching. Players need to be in the game

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.
