thinkphp use smarty third-party plug-in method to summarize _php instances

Source: Internet
Author: User

The example in this article describes the method used by thinkphp to use Smarty Third-party plug-ins. Share to everyone for your reference, specific as follows:

If you do not want to use the thinkphp frame of the template system, and the use of Third-party template system, you have a lot of other options, here I only introduce smarty this more official, and more powerful template system.

Because Smarty compatible PHP4, therefore, its efficiency will be relatively low a little bit, this low is only relative, estimated when Smarty officially give up PHP4 when the efficiency may be a big step.
Under the plugins directory of the TP framework, there is a smarttemplate directory, which is the Smarty plugin that comes with the system itself.

Use the following methods:

1, in the project's conf/config.php file add:

Return Array (
' think_plugin_on ' => true,
' Tmpl_engine_type ' => ' smarty '
);

2, download good smarty, and the Smarty Libs directory to the Project plugins directory (note, the plugins directory may not exist, you need to build), at the same time Libs directory renamed Smarttemplate (I hope it is not mistaken, In fact, it is the same as the Smartytemplate directory in the plugins directory of thinkphp, if you do not want to change the directory to the name, then, you must go to the TP directory to modify the plug-in file, so that it contains the correct path.

3, note that every time you modify the action or template file Delete temp under the HTML file

Explanation: The above content comes from the official, by Lin_chaoqi friend answers, the website is: Http://bbs.thinkphp.cn/viewthread.php?tid=305&highlight=smarty

The way I'm going to mention it here is not the same, black
Because when I use a third-party template plugin, Specially saw the TP the view.class.php discovered some very important question, that is, if the Third-party template plug-in, then the Third-party template plug-in efficiency may not be guaranteed, because the view class of the Fetch method in determining whether the Third-party Plug-ins, made a lot of TP template plug-in's own processing, and these for the use of the first Three-party template plug-ins, almost completely ineffective, these processes may affect Third-party plug-ins, but also affect the execution efficiency of Third-party plug-ins. The problem has been communicated with the fleeting, but due to changes may be very large, perhaps in recent versions, fleeting will not try to improve it, one is afraid of the impact of those who have used third-party plug-ins procedures, and if to remove these processing, then the view class may not need. Fleeting for such a situation should be unwilling to see. After all, this also affected the original system architecture, the estimated fleeting had to carefully consider ... [Of course, from the fleeting individual, certainly hope that we all use TP its own template plug-in, but I currently for the Smarty is more familiar with the], but for me this user, I need a temporary solution, so, there is the following content.

To solve this problem, I had to start with View.class.php because there was a line in Action.class.php:

$this->TPL = View::getinstance ();

So, in other words, the TPL variable is a single instance of View, checking the GetInstance method in View.class.php, and finding that it uses the GET_INSTANCE_OF function (this function is a little bug, not explained here. But I don't have a better solution at the moment, so I made changes to the getinstance and __construct two methods, removed the __construct method, added the Init method, and changed the code 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 ', ') ') {
$type = Ucfirst (Strtolower ($this->type));
Vendor ($type);
$type ();
$this;
    Return
  } public
function
    if (!empty
    }else{
    }
    if (!
        return new
    }


That is, let the view class invoke the Init method while instantiating it. In this way, I put my own template plugin under the directory of the third party Plug-ins (vendor).

Remember to remember: Do not miss the last return $this, in fact, this is what I call the get_instance_of bug, if not add this sentence, then when the type variable is PHP or think, getinstance can not be returned to the instance.

The new method of using the steps is as follows:

1, modify the project conf/config.php file:

Return Array (
' think_plugin_on ' => true,
' Tmpl_engine_type ' => ' Tpsmarty '
);

2, under the vendor directory of TP, create tpsmarty.php, the contents are as follows:

<?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
    }
}

The above is the simplest wording, in practice, these variables should be changed to your own site to match.

3, according to the Include_once function in the above file, copy the Smarty Libs directory to the plugins directory of the Project, renamed Smarty (only need to match the directory in include_once)

4, then, in the project method can be used directly:

Class Indexaction extends action{
index () {
$this->assign (' Test ', ' testss ');
$this->display (' default/index.html ');
    Public function
    }
}


Only, after using the plug-in, the display method parameters are the full path of the template, and can not be left blank (not unresolved, but to change the code will be more, the current method is the least modified).

Test, is it normal? Oh.

Now, let's change the template engine in config back to think, and set up the index directory in the tpl/default/directory, put the index.html on it, and modify the index () method above to add the original $this->display ('  Default/index.html ');  Changed to $this->display (); , try it, is it normal?

More interested in thinkphp related content readers can view the site topics: "thinkphp Introductory Course", "thinkphp Common Methods Summary", "Smarty Template Primer" and "PHP template technology Summary."

I hope this article will help you with the PHP program design based on thinkphp framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.