ThinkPHP使用Smarty第三方插件方法小结,thinkphpsmarty
ThinkPHP使用Smarty第三方插件方法小结,thinkphpsmarty
本文实例讲述了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程序设计有所帮助。
您可能感兴趣的文章:
- Thinkphp+smarty+uploadify实现无刷新上传
- ThinkPHP使用smarty模板引擎的方法
- 解决ThinkPHP下使用上传插件Uploadify浏览器firefox报302错误的方法
- ThinkPHP3.2.2的插件控制器功能
- ThinkPHP上使用多说评论插件的方法
- ThinkPHP3.2.2的插件控制器功能简述
- thinkPHP查询方式小结
- thinkphp命名空间用法实例详解
- thinkPHP下的widget扩展用法实例分析
- thinkphp实现无限分类(使用递归)
- Thinkphp实现自动验证和自动完成

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.

ThinkPHP6 backend management system development: Implementing backend functions Introduction: With the continuous development of Internet technology and market demand, more and more enterprises and organizations need an efficient, safe, and flexible backend management system to manage business data and conduct operational management. This article will use the ThinkPHP6 framework to demonstrate through examples how to develop a simple but practical backend management system, including basic functions such as permission control, data addition, deletion, modification and query. Environment preparation Before starting, we need to install PHP, MySQL, Com
