yii CI yaf 框架+smaryt模板

最近折腾了框架的性能测试,其中需要测试各个模板跟smarty配合的性能,所以折腾了一桶,现总结一下。之前已经写过 kohana框架+smaryt模板,这里不再重复了。

一,yii框架+smarty模板

        yii是覆盖了viewRenderer组件。

1.1,下载yii框架并解压,下载smarty框架并解压,将smarty/libs文件夹拷到yii框架application/protected/vendors下面,并重命名smarty。

1.2,yii配置文件main.php

'components'=>array(  
    'viewRenderer' => array(  
        'class'=>'batman.protected.extensions.SmartyViewRender',  
        // 这里为Smarty支持的属性  
        'config' => array (  
                'left_delimiter' => "{#",  
                'right_delimiter' => "#}",  
                'template_dir' => APP_DIR . "/views/",  
                'config_dir' => APP_DIR . "/views/conf/",  
                'debugging' => false,  
                'compile_dir' => 'D:/temp/runtime',    
        )   
    )  

其中batman是我已经在index.php定义好的别名。

Yii::setPathOfAlias('batman', dirname(__FILE__));
Yii::import("batman.protected.vendors.*");

define('APP_DIR', dirname(__FILE__).'/protected/');

1.3,在protected/extensions/下面新建SmartyViewRender.php

<?php
class SmartyViewRender extends CApplicationComponent implements IViewRenderer {
	public $fileExtension = '.html';
	private $_smarty = null;
	public $config = array();

	public function init() {
		$smartyPath = Yii::getPathOfAlias('batman.protected.vendors.smarty');
        Yii::$classMap['Smarty'] = $smartyPath . '/Smarty.class.php';
        Yii::$classMap['Smarty_Internal_Data'] = $smartyPath . '/sysplugins/smarty_internal_data.php';
        $this->_smarty = new Smarty();
        
		// configure smarty
		if (is_array ( $this->config )) {
			foreach ( $this->config as $key => $value ) {
				if ($key {0} != '_') { // not setting semi-private properties
					$this->_smarty->$key = $value;
				}
			}
		}
		
        Yii::registerAutoloader('smartyAutoload');

	}
	
	public function renderFile($context, $file, $data, $return) {
		 foreach ($data as $key => $value)
                $this->_smarty->assign($key, $value);
        $return = $this->_smarty->fetch($file);
        if ($return)
                return $return;
        else
                echo $return;
	}
}

1.4,验证

新建一个HelloController.php

<?php
class HelloController extends Controller {
	public function actionWorld() {
		$this->render('world', array('content'=>'hello world'));
	
	}
}

新建一个word.html

<body>
{#$content#}
</body>



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值