How PHP uses the smarty template in the Yii Framework

Source: Internet
Author: User
This article describes how to use the smarty template in the Yii Framework. For more information, see the first method.
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.

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 controller ID $ path = Yii: getPathOfAlias ('application '); // Obtain the absolute path include (dirname ($ path) of the protected folder ). 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 the 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 automatic loading function defined in YII will be displayed, and the SMARTY loading function will be executed.
Specifically, modify the autoload function in the YIIBase class and add the following code:

Public static function autoload ($ className) {// use include so that the error PHP file may appearif (preg_match ('/smarty/I', $ className )) {// if the class name contains smarty, it is returned regardless of case. in this way, the YII automatic loading class jumps out and the return is executed for the automatic loading class function of SMARTY ;} YII automatic loading class code}

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

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:

 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.

'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

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

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

For more details about how to use the smarty template in PHP based on the Yii Framework, refer to the PHP Chinese website!

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.