在YII框架中运用smarty模板引擎

基本方法:将Smarty以组件方式加入应用中。


一导入smarty核心包文件夹libs,此时Smarty.class.php路径如下:

---protected/extensions/smarty/libs/Smarty.class.php                                     

 

二新建一个类以继承核心类Smarty,主要是设置smarty的参数(包括四个目录和定界符),这里类名为YCSmarty,路径如下

--protected/extensions/smarty/YCSmarty.php                                           

该文件的全部代码如下

<?php                                                                          
require_once(Yii::getPathOfAlias('application.extensions.smarty.libs').DIRECTORY_SEPARATOR.'Smarty.class.php');                                                          
define('SMARTY_VIEW_DIR', Yii::getPathOfAlias('application.views'));                
class YCSmarty extends Smarty{                                                 
const DIR_SEP = DIRECTORY_SEPARATOR;                                 
                                                                        
public function __construct(){                                                
	parent::__construct();                                                   
	$this->template_dir = SMARTY_VIEW_DIR.self::DIR_SEP.'templates';          
	$this->compile_dir = SMARTY_VIEW_DIR.self::DIR_SEP.'template_c';          
	$this->config_dir = SMARTY_VIEW_DIR.self::DIR_SEP.'config';               
	$this->cache_dir = SMARTY_VIEW_DIR.self::DIR_SEP.'cache';                
	$this->left_delimiter = '<{';                                              
	$this->right_delimiter = '}>';      
}                                                                      
	public function init(){}                                                                       
}                                                                           
?>         


三修改配置文件protected/config/main.php,在组件项目添加YCSmarty类的配置,并在导入项目中增加一二步的所有文件导入配置。

此时配置文件如下:

return array(                                                                   
	'import'=>array(                                                               
		'application.extensions.*',                                                   
	),                                                                          
	'components'=>array(                                                           
		'smarty'=>array(                                                           
			'class' => 'application.extensions.smarty.YCSmarty',                        
		),                                                                     
	),                                                                        
);     


四在protected/views下新建四个文件夹templates,template_c,cache,config,此路径的配置在YCSmarty.php文件中。

 

五测试在控制器中就可以用$smarty = Yii::app()->smarty来获取到自己定义的YCSmarty这个对象,注意的是组件表现为单例模式,smarty就是它的ID。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值