手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表2014年11月20日的文章

Yii2-smarty的一些小坑

在写本文前我不得不说一句,其实我是不想用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

原来是:

PHP代码
  1. public function functionRegisterJsFile($params$template)  
  2. {  
  3.     if (!isset($params['url'])) {  
  4.         trigger_error("registerJsFile: missing 'url' parameter");  
  5.     }  
  6.   
  7.     $url = ArrayHelper::remove($params'url');  
  8.     $key = ArrayHelper::remove($params'key', null);  
  9.     $depends = ArrayHelper::remove($params'depends', null);  
  10.     if (isset($params['position']))  
  11.         $params['position'] = $this->getViewConstVal($params['position'], View::POS_END);  
  12.   
  13.     Yii::$app->getView()->registerJsFile($url$depends$params$key);  
  14. }  

改成为:

PHP代码
  1. /** 
  2.  * Smarty function plugin 
  3.  * Usage is the following: 
  4.  * 
  5.  * {registerJsFile url='http://maps.google.com/maps/api/js?sensor=false' position='POS_END'} 
  6.  * 
  7.  * Supported attributes: url, key, depends, position and valid HTML attributes for the script tag. 
  8.  * Refer to Yii documentation for details. 
  9.  * The position attribute is passed as text without the class prefix. 
  10.  * Default is 'POS_END'. 
  11.  * 
  12.  * @param $params 
  13.  * @param \Smarty_Internal_Template $template 
  14.  * @return string 
  15.  * @note Even though this method is public it should not be called directly. 
  16.  */  
  17. public function functionRegisterJsFile($params$template)  
  18. {  
  19.     if (!isset($params['url'])) {  
  20.         trigger_error("registerJsFile: missing 'url' parameter");  
  21.     }  
  22.   
  23.     $url = ArrayHelper::remove($params'url');  
  24.     $key = ArrayHelper::remove($params'key', null);  
  25.     $params['depends'] = ArrayHelper::remove($params'depends', null);  
  26.     if (isset($params['position']))  
  27.         $params['position'] = $this->getViewConstVal($params['position'], View::POS_END);  
  28.   
  29.     Yii::$app->getView()->registerJsFile($url$params$key);  
  30. }  

其实就是$params['depends']这个参数。registerJsFile只能接受3个参数,但事实上用了4个参数,所以调整一下即可

 

Tags: yii2, smarty, twig