Yii Framework integration Smarty template description

Source: Internet
Author: User
In the protected Directory, create a folder named "vendor/smarty" and put the smarty class package into it.

1. create a folder named vendor/smarty under the protected directory and put the smarty class package into it.

2. create the file Csmarty. php under the extensions directory.

The content of the Csmarty. php file is as follows (meaning you can understand it ):

 template_dir =SMARTY_VIEW_DIR.self::DS.'tpl';
 $this->compile_dir =SMARTY_VIEW_DIR.self::DS.'tpl_c'; $this->caching = true; $this->cache_dir =SMARTY_VIEW_DIR.self::DS.'cache'; $this->left_delimiter =  '{'; $this->right_delimiter = '}'; $this->cache_lifetime = 3600;     }     function init() {}}


3. create a folder based on the CSmarty. php code.

4. Master configuration file settings

Open protected/config/main. php

Add to the components array


'smarty'=>array(    'class'=>'application.extensions.CSmarty',   ),

In the controller:


5. get the smarty instance


Yii: app ()-> smarty (); instance: public function actionIndex () {Yii: app ()-> smarty ()-> assign ('name ', 'Zhang San'); //. directory of tpl protected/views/smarty/tpl/. tpl, Code {$ name} Yii: app ()-> smarty ()-> display ('A. tpl ');}

At this time, the system reports an error: cause:

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.

Solution:

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:


Public static function autoload ($ className) {// if the class name contains smarty, return regardless of case, // This will jump out of YII automatic loading class and execute the // SMARTY automatic loading class function if (preg_match ('/smarty/I', $ className) {return ;}
Test again :.....


This is OK

6. Optimization

In the action, you can use Yii: app ()-> smarty to try smarty.

If you use Yii: app ()-> smarty in action every time,

It can be added to the Controller under components.


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

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

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.