Thinkphp using smarty third-party plug-in method summary _php instance

Source: Internet
Author: User
The examples in this article describe how thinkphp uses smarty third-party plug-ins. Share to everyone for your reference, as follows:

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

Because Smarty compatible PHP4, therefore, its efficiency will be relatively low a little bit, this low is only relative, estimate such smarty when formally give up PHP4, 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.

Here's how to use it:

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 plugins directory of the project (to illustrate, plugins directory may not exist, need to establish themselves), while the Libs directory renamed to Smarttemplate (I hope it is not mistaken, In fact, and thinkphp plugins directory in the Smartytemplate directory of the same name can be), if you do not want to change the directory to this name, then you must go to the TP plug-in directory to modify the plugin file, so that it contains the correct path.

3. Note Modify the HTML file under temp Every time you modify the action or template file

Description: The above content from the official, by the Lin_chaoqi friend answer, the site 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 and black.
Because I'm using a third-party template plugin, Specifically see TP view.class.php found some very important problem, that is, if the third-party template plug-in, then the efficiency of the third-party template plug-in may not be guaranteed, because the view class of the Fetch method in judging whether it is a third-party plug-in, made a lot of TP template plug-in processing, and these for use The three-party template plug-in, almost completely invalid, these processing 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 because the changes may be very large, perhaps in recent versions, the fleeting will not try to improve it, one is afraid to affect those who already use the third-party plug-in programs, and secondly, if you remove these processing, then the view class may not need. The fleeting circumstances should be unwilling to see such a situation. After all, this also affected the original system architecture, estimated that the fleeting need to carefully consider ... [Of course, from the fleeting individual, it is certainly hoped that we all use TP own template plug-in, but I am now more familiar with smarty, but for me this user, I need is a temporary solution, so, there is the following content.

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

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

So, that is, the TPL variable is the singleton mode of the View, check the View.class.php in the GetInstance method, found that the use of get_instance_of this function (this function is a little bit bug, here does not explain, But I don't have a better solution at the moment, so I changed 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 ', ' 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 called at the same time. In this method, I put my own template plugin into the third-party plugin directory (Vendor).

Remember: Do not miss the last sentence return $this, in fact, this is what I said get_instance_of bug, if not added, then when the type variable is PHP or think, getinstance is unable to return the instance.

The new usage steps are 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 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    }}

Above is the simplest notation, in practical use, these variables should be replaced with 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 method of the project can be directly used:

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's parameters are the full path of the template, and can not be left blank (not to be solved, but to change the code will be more, the current method is the least change).

Test it, is it normal? Oh.

Now, we put the template engine in config back to think, at the same time in the tpl/default/directory to establish the index directory, put index.html, and modify the index () method above, the original $this->display (' Default/index.html '); Change to $this->display (); , try it, is it normal?

More interested in thinkphp related content readers can view this site topic: "thinkphp Introductory Tutorial", "thinkphp Common Methods Summary", "Smarty Template Primer Basic Tutorial" and "PHP template technology Summary."

It is hoped that this article is helpful to 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.