本文实例讲述了yii,CI,yaf框架+smarty模板使用方法。分享给大家供大家参考,具体如下:
最近折腾了框架的性能测试,其中需要测试各个模板跟smarty配合的性能,所以折腾了一桶,现总结一下。之前已经写过kohana框架+smarty模板,这里不再重复了。
一、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
<?phpclass 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
<?phpclass HelloController extends Controller { public function actionWorld() { $this->render('world', array('content'=>'hello world')); }}
新建一个word.html
<body>{#$content#}</body>
二、CI框架+smarty模板
网上很多方法,将smarty作为一个普通的library,在使用的时候,controller代码类似于下面:
public function index(){ $this->load->library('smarty/Ci_smarty', '', 'smarty'); $this->smarty->assign("title","恭喜你smarty安装成功!"); $this->smarty->assign("body","欢迎使用smarty模板引擎"); $arr = array(1=>'zhang',2=>'xing',3=>'wang'); $this->smarty->assign("myarray",$arr); $this->smarty->display('index_2.html');}
新闻热点
疑难解答