最近折腾了框架的性能测试,其中需要测试各个模板跟smarty配合的性能,所以折腾了一桶,现总结一下。之前已经写过
kohana框架+smaryt模板,这里不再重复了。
1.3,在protected/extensions/下面新建SmartyViewRender.php
1.4,验证
新建一个word.html
一,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>