This article mainly introduces ThinkPHP's method of using the Smarty third-party plug-in, and summarizes and analyzes the specific steps and precautions of ThinkPHP's use of the Smarty template in combination with the instance form, for more information about how ThinkPHP uses the Smarty third-party plug-in, see the example below. We will share this with you for your reference. The details are as follows:
If you do not want to use the template system that comes with TP 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 a powerful template system.
Because Smarty is compatible with PHP4, its efficiency will be relatively low. this is only relative. it is estimated that when Smarty officially gives up PHP4, the efficiency may be greatly improved.
There is a SmartTemplate directory under the PlugIns directory of the TP framework, which is the Smarty plug-in that comes with the system.
The usage is as follows:
1. add the following in the Conf/Config. php file of the project:
return array('THINK_PLUGIN_ON' => true,'TMPL_ENGINE_TYPE'=>'smarty',);
2. download Smarty and copy the libs directory of smarty to the PlugIns directory of the project (note that the PlugIns directory may not exist and must be created by yourself ), at the same time, you can change the libs directory to SmartTemplate (which is the same as the SmartyTemplate directory in the THINKPHP PlugIns directory). If you do not want to change the directory to this name, you must modify the plug-in file in the TP plug-in directory to make the inclusion path correct.
3. modify the action or template file and delete the html file under Temp.
Note: the above content from the official, by lin_chaoqi friends to answer, Web site: http://bbs.thinkphp1.cn/viewthread.php? Tid = 305 & highlight = smarty
The method I want to mention here is different from above, black and black
This is because when I used a third-party template plug-in, I specially read the TP view. class. php has discovered some important problems, 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 performs a lot of self-owned TP template plug-ins to determine whether it is a third-party plug-in, and these are almost completely ineffective for using third-party template plug-ins, these operations may affect third-party plug-ins and affect the execution efficiency of third-party plug-ins. The problem has been discussed with Ruian, but the changes may be very large. maybe in recent versions, Ruian will not try to improve it, first, they are afraid of affecting those programs that have used third-party plug-ins. Second, if they are removed, the View class may not be needed. The current year should be reluctant to see such a situation. After all, this also affects the architecture of the original system. it is estimated that the current year has to be carefully considered ...... [For example, if you are a user of the current year, you certainly want to use the template plug-in provided by TP, but I am more familiar with smarty.] but for me, what I need is a temporary solution, so I have the following content.
To solve this problem, I only need to start from View. class. php, because Action. class. php contains a line:
$this->tpl = View::getInstance();
That is to say, the variable tpl is the singleton mode of View. check the View. class. the getInstance method in php uses the get_instance_of function (this function has a small BUG and is not explained here, but I have no better solution at present ), so I modified the getInstance and _ construct methods, deleted the _ construct method, and added the init method. the changed 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, when the View class is instantiated, the init method is also called. In this method, I put my own template plug-ins under the third-party plug-ins Directory (Vendor.
Remember: never miss the last return $ this;. In fact, this is what I call the get_instance_of BUG. if this is not added, when the type variable is PHP or THINK, getInstance cannot return an instance.
The new procedure is as follows:
1. modify the Conf/Config. php file of the project:
return array('THINK_PLUGIN_ON' => true,'TMPL_ENGINE_TYPE'=>'TpSmarty',);
2. create TpSmarty. php under the TP Vendor Directory. the content is as follows:
<?phpinclude_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 method. in actual use, please replace these variables with your own site.
3. copy the libs directory of smarty to the PlugIns directory of the project based on the include_once function in the above file and change it to smarty (only matching the Directory in include_once)
4. then, you can directly use the following methods in the project:
class IndexAction extends Action{index(){$this->assign('test','testss');$this->display('default/index.html'); public function }}}
However, after the plug-in is used, the parameters of the display method are the full path of the template and cannot be left blank. (this is not the solution, but the code to be modified will be more, currently, this method is the least changed ).
Test whether it is normal? Haha.
Now, we can change the template engine in Config back to Think. at the same time, we can create an index directory under the Tpl/default/directory, put index.html in it, and modify the index () method above, change the original $ this-> display ('default/index.html '); to $ this-> display ();. can you give it a try?