Onethink thinkphp integrates notes of the smarty template engine

Source: Internet
Author: User
Tags smarty template


I recently used the onethink officially published by thinkphp to set up the management backend. Because the company's previous code was written by smarty and there were more than 2000 templates, I was too lazy to use the think template engine to change them,

Therefore, the smarty template engine is integrated and compatible with think templates.

I. Add Application/Common/Conf/config. php

'Tmpl _ ENGINE_TYPE '=> 'smarty ',

II. Download the smarty template engine

Http://pan.baidu.com/s/1ntoXvwH

Under ThinkPHP \ Library \ Vendor

Because onethink already has a smarty class

ThinkPHP/Library/Think/Template/Driver/Smarty. class. php

Public function fetch ($ templateFile, $ var ){
$ TemplateFile = substr ($ templateFile, strlen (THEME_PATH ));
Vendor ('smarty. Smarty # class ');
$ Tpl = new \ Smarty ();
$ Tpl-> caching = C ('tmpl _ CACHE_ON ');
$ Tpl-> template_dir = THEME_PATH;
$ Tpl-> compile_dir = CACHE_PATH;
$ Tpl-> cache_dir = TEMP_PATH;
If (C ('tmpl _ ENGINE_CONFIG ')){
$ Config = C ('tmpl _ ENGINE_CONFIG ');
Foreach ($ config as $ key => $ val ){
$ Tpl-> {$ key} = $ val;
}
}
$ Tpl-> assign ($ var );
$ Tpl-> display ($ templateFile );
}

The onethink template parsing process is in

HinkPHP \ Library \ Behavior \ ParseTemplateBehavior. class. php

Public function run (& $ _ data ){
$ Engine = strtolower (C ('tmpl _ ENGINE_TYPE '));
$ _ Content = empty ($ _ data ['content'])? $ _ Data ['file']: $ _ data ['content'];
$ _ Data ['prefix'] =! Empty ($ _ data ['prefix'])? $ _ Data ['prefix']: C ('tmpl _ cache_prefix ');
If ('think '= $ engine) {// use the think template engine
If ((! Empty ($ _ data ['content']) & $ this-> checkContentCache ($ _ data ['content'], $ _ data ['prefix'])
| $ This-> checkCache ($ _ data ['file'], $ _ data ['prefix']) {// The cache is valid.
// Load the template cache file
Storage: load (C ('cache _ path '). $ _ data ['prefix']. md5 ($ _ content ). C ('tmpl _ CACHFILE_SUFFIX '), $ _ data ['var']);
} Else {
$ Tpl = Think: instance ('think \ template ');
// Compile and load the template file
$ Tpl-> fetch ($ _ content, $ _ data ['var'], $ _ data ['prefix']);
}
} Else {
// Call the third-party template engine for parsing and output
If (strpos ($ engine ,'\\')){
$ Class = $ engine;
} Else {
$ Class = 'think \ Template \ Driver \ '. ucwords ($ engine );
}
If (class_exists ($ class )){
$ Tpl = new $ class;
$ Tpl-> fetch ($ _ content, $ _ data ['var']);
} Else {// The class is not defined
E (L ('_ NOT_SUPPERT _'). ':'. $ class );
}
}
}

It can be seen that if the template engine is not think, it will be instantiated.

'Think \ Template \ Driver \ '. ucwords ($ engine );

In this way, your system can support the smarty template,

However, some of your other templates do not want to use smarty or retain think.

You can add a keyword in config. php.


If (strstr ($ _ GET ['s '], "NoSmarty ")! = ''){){
 
Return array (
 
'Tmpl _ ENGINE_TYPE '=> 'smarty ',
 
// The current controller uses smarty
 
);
 
} Else {
 
Return array (
 
// The current controller does not use smarty
 
);
 
}

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.