Home > Backend Development > PHP Tutorial > Detailed explanation of how to use smarty templates in PHP based on Yii framework_PHP tutorial

Detailed explanation of how to use smarty templates in PHP based on Yii framework_PHP tutorial

WBOY
Release: 2016-07-21 15:07:14
Original
861 people have browsed it

The first method
It is a bit troublesome to generate views according to the YII system method, and I think it is easier to use smarty. Tried adding smarty templates.

Copy code The code is as follows:

date_default_timezone_set("PRC");
class PlaceController extends CController {
protected $_smarty;
function __construct(){
parent::__construct('place');//You need a parameter to call the constructor of the parent class, which is the controller ID
$path = Yii::getPathOfAlias('application');//Get the absolute path of the protected folder
include (dirname($path).DIRECTORY_SEPARATOR.'Smarty'.DIRECTORY_SEPARATOR.'Smarty.class.php');//smarty Path
$this->_smarty = new Smarty();
$this->_smarty->template_dir = dirname($path).DIRECTORY_SEPARATOR.'template'.DIRECTORY_SEPARATOR;//Template path
}

The main problem is the execution conflict of automatically loaded classes.
YII registered an autoloading classspl_autoload_register(array('YiiBase','autoload')), SMARTYalso registered an autoloading class, spl_autoload_register('smartyAutoload'),YII Register first, so that when a class name is encountered, YII's custom auto-loading class function is executed first. For each class name in SMARTY, YII's auto-loading class is also called first. function, but if it does not meet the conditions for YII auto-loading, SMARTY's auto-loading class function will be executed. However, SMARTY's class name does comply with the logical statement of YII's auto-loading class when automatically loading the class. As a result That is, the class to be included using the Include statement in YII will definitely not be found.
The solution is: When the SMARTY class is automatically loaded, jump out of the automatic loading function defined in YII, so that the SMARTY loading function will be executed.
The specific implementation is to modify the autoload function in the YIIBase class and add the following code
Copy the 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)){ //As long as the class If the name contains smarty, it will be returned regardless of case. This will jump out of the YII automatic loading class and execute the SMARTY automatic loading class function
re turn;
}
YII automatic loading class code
}

In this way, you can use the smarty template in each Action.
Copy code The code is as follows:

public function actionIndex(){
$this->_smarty-> assign('test', 'Test');
$this->_smarty->display('create.html');
}

Second method:
Put the smarty template plug-in in the extensions folder under protected and create a CSmarty class file with the following content
Copy the code 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() {} 
    } 
    ?>

然后建立samrty所需的template_c,cache等文件夹。
接下来是配置部分
打开protected/config/main.php在components数组中加入
复制代码 代码如下:

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

最后在action中直接用Yii::app()->smarty就可以试用smarty了。如果每次在action中使用Yii::app()->smarty比较麻烦的话,可以在components下的Controller中可以加入
复制代码 代码如下:

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

然后在action中就直接可以用$this->smarty使用smarty了。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/327564.htmlTechArticle第一种方法 按照YII系统的办法生成视图觉得有点麻烦,觉得用smarty更省事。尝试着把smarty模板加进来了。 复制代码 代码如下: date_default_...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template