在写本文前我不得不说一句,其实我是不想用smarty的,我想尝试一下twig,但是phpstorm的twig插件真要命,卡成翔,所以我只能用smarty。为什么不用prado了呢?官方说不支持了,我晶啊 在使用smarty的时候官方的代码和例子看上去很美,不过要注意几点 1、用yii
在写本文前我不得不说一句,其实我是不想用smarty的,我想尝试一下twig,但是phpstorm的twig插件真要命,卡成翔,所以我只能用smarty。为什么不用prado了呢?官方说不支持了,我晶啊
在使用smarty的时候官方的代码和例子看上去很美,不过要注意几点
1、用yii2-smarty,还是必须得用layout,如果你不支持layout文件,默认就是/layouts/main.php,天啊,为什么是php?而且在这里面也还真的能用php代码。整个都崩溃了
2、你可以指定layout文件,比如:main.tpl,ok你必须得象php文件一样,得写{$this->head()},{$this->startbody()}{$this->endpage()}等,否则 clientscript功能就无法使用
3、如果你指定layout=false,那么,就不支持clientscript了。因为你incude file='xxx.tpl',在每一个独立的文件里都必须要象2中一个个的this->head(),this->endpage全写上
4、再来一个bug:{registerjsfile url=''},这个函数有bug
原来是:
public function functionregisterjsfile($params, $template) { if (!isset($params['url'])) { trigger_error(registerjsfile: missing 'url' parameter); } $url = arrayhelper::remove($params, 'url'); $key = arrayhelper::remove($params, 'key', null); $depends = arrayhelper::remove($params, 'depends', null); if (isset($params['position'])) $params['position'] = $this->getviewconstval($params['position'], view::pos_end); yii::$app->getview()->registerjsfile($url, $depends, $params, $key); }
改成为:
/** * smarty function plugin * usage is the following: * * {registerjsfile url='http://maps.google.com/maps/api/js?sensor=false' position='pos_end'} * * supported attributes: url, key, depends, position and valid html attributes for the script tag. * refer to yii documentation for details. * the position attribute is passed as text without the class prefix. * default is 'pos_end'. * * @param $params * @param \smarty_internal_template $template * @return string * @note even though this method is public it should not be called directly. */ public function functionregisterjsfile($params, $template) { if (!isset($params['url'])) { trigger_error(registerjsfile: missing 'url' parameter); } $url = arrayhelper::remove($params, 'url'); $key = arrayhelper::remove($params, 'key', null); $params['depends'] = arrayhelper::remove($params, 'depends', null); if (isset($params['position'])) $params['position'] = $this->getviewconstval($params['position'], view::pos_end); yii::$app->getview()->registerjsfile($url, $params, $key); }
其实就是$params['depends']这个参数。registerjsfile只能接受3个参数,但事实上用了4个参数,所以调整一下即可
原文地址:yii2-smarty的一些小坑, 感谢原作者分享。