PHP is based on the smarty template used in the Yii framework

  • 2020-06-12 08:40:33
  • OfStack

The first method
Generating views the way the YII system does is a bit of a hassle, and smarty is much easier. Try adding smarty templates.

class PlaceController extends CController {
protected $_smarty;
function __construct(){
parent::__construct('place');// Need to be 1 Call the constructor of the parent class with the parameters of the controller ID
$path = Yii::getPathOfAlias('application');// To obtain protected The absolute path to the folder 
include (dirname($path).DIRECTORY_SEPARATOR.'Smarty'.DIRECTORY_SEPARATOR.'Smarty.class.php');//smarty In the path 
$this->_smarty = new Smarty();
$this->_smarty->template_dir = dirname($path).DIRECTORY_SEPARATOR.'template'.DIRECTORY_SEPARATOR;// The template path 

One of the main problems is the problem of auto-loading class execution conflicts.
YII registered 1 auto-load class spl_autoload_register(array('YiiBase','autoload'),SMARTY also registered 1 auto-load class, spl_autoload_register('smartyAutoload'),YII registered before, so when encountering 1 class name, it first executes YII's custom auto-load class function, corresponding to each class name in SMARTY, it is also the function of calling YII's auto-load class first. However, if the condition of YII autoload is not met, the function of SMARTY autoload will be executed. However, the class name of SMARTY does meet the logic statement of YII autoload when the class is automatically loaded, and the result is that the class that YII USES Include statement to contain is definitely not found.
The solution: When SMARTY's classes are automatically loaded, jump out of the auto-load function defined in YII, and the loading function of SMARTY is executed.
The implementation is to modify the autoload function in the YIIBase class to add the following code

public static function autoload($className)
// use include so that the error PHP file may appear
if(preg_match('/smarty/i', $className)){      // As long as the class name contains smarty Returns, regardless of case, and it pops out YII Automatically loads the class to execute                                                                                   SMARTY Autoload class functions 
             YII Automatically loads the class code 

This makes it possible to use the smarty template in each Action.

public function actionIndex(){
$this->_smarty->assign('test', ' test ');

The second method:
Place the extensions folder under protected into the smarty template plug-in and create the CSmarty class file, as follows

    define('SMARTY_VIEW_DIR', Yii::getPathOfAlias('application.views'));  

    class CSmarty extends Smarty {  
        function __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->right_delimiter =  '}-->';  
            $this->cache_lifetime = 3600;  
        function init() {}  

Then create the folders template_c, cache, etc. required by samrty.
Next comes the configuration section
Open protected/config/main php into components array


Finally, Yii::app()- is directly used in action > smarty you can try smarty. If you use Yii::app()- each time in action > If smarty is more troublesome, it can be added to Controller under components

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

Then in action you can just use $this- > smarty USES smarty.

Related articles: