Home >Backend Development >PHP Tutorial >Summary of how ThinkPHP uses Smarty third-party plug-ins, thinkphpsmarty_PHP tutorial

Summary of how ThinkPHP uses Smarty third-party plug-ins, thinkphpsmarty_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:56:37751browse

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