Yii framework

Yii整合smarty

Yii整合smarty

1.下载Smarty http://www.smarty.net/download

2.解压出下载好的Smarty-x.x.x.zip中的libs文件夹,放到Yii项目中的protected/extentions目录下,并更名为smarty

3.在protected/extentions目录下新建CSmarty.php类文件

<?php
template_dir = SMARTY_TEMPLATE_DIR;//template dir
		$this->compile_dir = SMARTY_VIEW_DIR.DIRECTORY_SEPARATOR.'template_c'; //teplate_c dir
		$this->caching = false; //disable cache
		//$this->cache_dir = SMARTY_VIEW_DIR.DIRECTORY_SEPARATOR.'cache'; //cache dir
		$this->left_delimiter  =  '<{';//delimiter start
		$this->right_delimiter =  '}>';//delimiter end
		$this->cache_lifetime = 3600;
	}
	
	function init() {
		Yii::registerAutoloader('smartyAutoload');
	}
}

 

4.在Yii项目文件夹protected下创建模板目录template
5.修改配置文件protected/config/main.php,在components中添加以下内容

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

 

6.编写测试action和模板文件
测试action

    /**
     * smarty 测试
     */
    public function actionSmartytest(){
	$data = array('allen','lancy','joy');
	Yii::app()->smarty->assign('data',$data);
	Yii::app()->smarty->display('thread/smartytest.tpl');
    }

 

模板文件smartytest.tpl

<{foreach from=$data item=item}>
    <{$item}>
<{/foreach}>

 

测试结果如下:
allen
lancy
joy

%1 $ S

发表回复