Yii1.1整合smarty模板以及使用

本文详细介绍了如何将Smarty模板整合进Yii1.1框架,包括配置步骤、关键代码解析及模板使用方法。

Yii1.1整合smarty模板

法一:

一,下载smarty3.X到本地,放到Yii框架 /protected/extensions/目录下


这里多余的smarty包里的demo和非重要文件都没有删除。

***要非常注意Smarty.class.php 的位置,这里有对其他php类文件的路径定义变量,如果改变了Smarty.class.php 位置,记得一定要修改该文件中路径变量,否则再引入sysplugins包中的php时 会报:找不到资源的错误!!***

二,在/protected/extensions/目录下创建CSmarty.php文件

<?php
define('SMARTY_SPL_AUTOLOAD', true);
require_once (Yii::getPathOfAlias('application.extensions.smarty').DIRECTORY_SEPARATOR.'libs'.DIRECTORY_SEPARATOR.'Smarty.class.php'); //smarty配置
define('SMARTY_VIEW_DIR', Yii::getPathOfAlias('application.views.sblog.smarty'));

class CSmarty extends Smarty{
    public $_smarty;
function __construct() {
    	parent::__construct();
        $this->template_dir = SMARTY_VIEW_DIR.DS.'tpl';
        $this->compile_dir = SMARTY_VIEW_DIR.DS.'tpl_c';
        $this->caching = false;
        $this->cache_dir = SMARTY_VIEW_DIR.DS.'cache';
        $this->config_dir = SMARTY_VIEW_DIR.DS.'config';
        $this->cache_lifetime = 3600;
    }
	function init(){
//		Yii::registerAutoloader('smartyAutoload');
        
    }
}
?>

注意两点:

1Smarty.class.php的路径,根据自己的实际情况来写

2SMARTY_VIEW_DIR 这个是smarty的模板起作用的路径,可以随意指定。

3DS 是路径分隔符’/’

三,在/protected/config/main.php 文件中加

	'components'=>array(

		'user'=>array(
			// enable cookie-based authentication
			'allowAutoLogin'=>true,
		),

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

这样就把smarty整合到Yii1.1里了。


如何使用呢?

一,在/protected/components/Controller.php 文件中,加

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

二,在/protected/controllers/XXXController.php中的actionXXX函数中

		$world = "lily";
		$this->smarty->assign( 'world', $world );
		$this->smarty->display( 'test.html');
test.html 在smarty模板目录中建立。









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值