How to use the smarty template in the Yii Framework of PHP _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP uses the smarty template based on the Yii Framework. The first method is to generate a view based on the YII system. it is a little troublesome and it is easier to use smarty. Try to add the smarty template. Copy the code as follows: date_default _ Method 1
Using the YII system to generate a view is a little troublesome and it is easier to use smarty. Try to add the smarty template.

The code is as follows:


Date_default_timezone_set ("PRC ");
Class PlaceController extends CController {
Protected $ _ smarty;
Function _ construct (){
Parent: :__ construct ('Place'); // A parameter is required to call the constructor of the parent class. the parameter is the controller ID.
$ Path = Yii: getPathOfAlias ('application'); // Obtain the absolute path of the protected folder.
Include (dirname ($ path). DIRECTORY_SEPARATOR. 'smarty '. DIRECTORY_SEPARATOR. 'smarty. class. php'); // path of Smarty
$ This-> _ smarty = new Smarty ();
$ This-> _ smarty-> template_dir = dirname ($ path). DIRECTORY_SEPARATOR. 'template'. DIRECTORY_SEPARATOR; // template path
}


One major problem is Automatic loading class execution conflict.
YII registers an automatic loading class spl_autoload_register (array ('yibabase', 'autoload'), and SMARTY also registers an automatic loading class, spl_autoload_register ('smartyautoload '), YII is registered first. in this way, when a class name is encountered, the YII custom auto-load class function is executed first, which corresponds to each class name in SMARTY, it also calls YII's automatic loading class function first. However, if YII's automatic loading condition is not met, the SMARTY's automatic loading class function will be executed. However, when the class name of SMARTY is automatically loaded, it does conform to the logic statement of YII auto-loading class. The result is that the class to be included in YII's Include statement cannot be found.
The solution is:When the SMARTY class is automatically loaded, the automatically loaded function defined in YII will be executed.
Specifically, modify the autoload function in the YIIBase class and add the following code:

The code is as follows:


Public static function autoload ($ className)
{
// Use include so that the error PHP file may appear
If (preg_match ('/smarty/I', $ className) {// if the class name contains smarty, it is returned regardless of case, in this way, YII automatically loads the class and executes the automatic loading class function of SMARTY.
Return;
}
YII automatically loads class code
}


In this way, you can use the smarty template in each Action.

The code is as follows:


Public function actionIndex (){
$ This-> _ smarty-> assign ('test', 'test ');
$ This-> _ smarty-> display('create.html ');
}


Method 2:
Put the extensions folder under protected into the smarty template plug-in and create a CSmarty class file. the content is as follows:

The code is as follows:


Require_once (Yii: getPathOfAlias ('application. extensions. smarty '). DIRECTORY_SEPARATOR. 'smarty. class. php ');
Define ('smarty _ VIEW_DIR ', Yii: getPathOfAlias ('application. views '));

Class CSmarty extends Smarty {
Const DIR_SEP = DIRECTORY_SEPARATOR;
Function _ construct (){
Parent: :__ construct ();

$ This-> template_dir = SMARTY_VIEW_DIR;
$ This-> compile_dir = SMARTY_VIEW_DIR.self: DIR_SEP. 'Template _ C ';
$ This-> caching = true;
$ This-> cache_dir = SMARTY_VIEW_DIR.self: DIR_SEP. 'cache ';
$ This-> left_delimiter =' ';
$ This-> cache_lifetime = 3600;
}
Function init (){}
}
?>


Create the template_c, cache, and other folders required by samrty.
Next is the configuration section
Open protected/config/main. php and add it to the components array.

The code is as follows:


'Smarty '=> array (
'Class' => 'application. extensions. CSmarty ',
),


Finally, you can use Yii: app ()-> smarty in the action to try smarty. If you use Yii: app ()-> smarty in action every time, you can add

The code is as follows:


Protected $ smarty = '';
Protected function init (){
$ This-> smarty = Yii: app ()-> smarty;
}


Then, you can use $ this-> smarty to use smarty in the action.

Using the YII system to generate a view is a little troublesome and it is easier to use smarty. Try to add the smarty template. The code is as follows: date_default _...

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.