Yii widget使用

关于widgets,他们在yii中的关系如下 
system.web.widgets 系统自带最基本的widget 
zii.widgets 是基本扩展 
zii.widgets.grid 是基本扩展的重要分支 
zii.widgets.jui 是插件扩展 

CWidget:http://www.yiiframework.com/doc/api/1.1/CWidget/

CWidget is the base class for widgets. 

A widget is a self-contained component that may generate presentation based on model data. It can be viewed as a micro-controller that embeds into the controller-managed views

Compared with controller, a widget has neither actions nor filters. 

Usage is described at CBaseController and CBaseController::widget.

下面以一个随机广告图片为例说明Yii中Widget的用法 
1. 调用Widget 

Php代码  收藏代码
  1. <?php $this->widget('WidgetName'); ?>  


或者 

Php代码  收藏代码
    1. <?php $widget=$this->beginWidget('path.to.WidgetClass'); ?>  
    2. ...可能会由小物件获取的内容主体...  
    3. <?php $this->endWidget(); ?>  
也可以传参到Widget类 
Php代码  收藏代码
  1. <?php $userId = 1; ?>  
  2. <?php $this->widget('WidgetName',array('userId'=>$userId)); ?>  

参数userId自动映射到Widget类的同名属性,所以在定义Widget时,别忘记了声明该属性。 

2. 创建Widget 
自定义Widget类要继承CWidget,覆盖方法run 
Php代码  收藏代码
  1. <?php  
  2. class BannerMagic extends CWidget {  
  3.     public function run(){  
  4.     }  
  5. }  

或者: 
 
  1. class MyWidget extends CWidget {  
  2.     public function init() {  
  3.         // 此方法会被 CController::beginWidget() 调用  
  4.     }  
  5.      public function run() {  
  6.         // 此方法会被 CController::endWidget() 调用  
  7.     }  
  8. }  

下面是是BannerMagicWidget实现 
Php代码  收藏代码
  1. <?php class BannerMagicWidget extends CWidget {  
  2.    public function run() {  
  3.      $random = rand(1,3);  
  4.      if ($random == 1) {  
  5.        $advert = "advert1.jpg";  
  6.      }  else if ($random == 2) {  
  7.        $advert = "advert2.jpg";  
  8.      }  else {  
  9.        $advert = "advert3.jpg";  
  10.      }   
  11.      $this->render('bannermagic',array(  
  12.        "advert"=>$advert,  
  13.      ));  
  14.    }  
  15. }  

存储到protectedcomponentsBannerMagicWidget.php 

对应的view文件可能的内容如下: 
Php代码  收藏代码
  1. <img src="images/adverts/<?php echo $advert; ?>" alt="whatever" />  

存储到protectedcomponentsviewsannermagic.php 

3. 调用该Widget 
Php代码  收藏代码
  1. <?php $this->widget('BannerMagicWidget'); ?>  

转自:http://koda.iteye.com/blog/1134606

yii dropdownlisthttp://www.yiiframework.com/doc/api/1.1/CHtml#dropDownList-detail

public static string dropDownList(string $name, string $select, array $data, array $htmlOptions=array ( ))

 
 
Zii组件中包含了一些基于JQuery的UI组件,这些UI组件定义在包zii.widgets.jui中,包括CJuiAccordion ,CJuiAutoComplete,CJuiDatePicker等。本篇介绍CJuiAccordion,显示一个Accordion组件(类似手风琴可以折叠的UI组件)。这个控件封装了 JUI Accordion插件。
基本用法如下:
 
 
[php] 
<?php  
$this->widget('zii.widgets.jui.CJuiAccordion', array(  
    'panels'=>array(  
                'panel 1'=>'Content for panel 1',  
                'panel 2'=>'Content for panel 2',  
                'panel 3'=>$this->renderPartial('_content1',null,true),  
                ),  
            'options'=>array(  
                'collapsible'=>true,  
                'active'=>1,  
                ),  
            'htmlOptions'=>array(  
                'style'=>'500px;'  
                ),  
            ));  
  
?>  
 
<?php
$this->widget('zii.widgets.jui.CJuiAccordion', array(
'panels'=>array(
'panel 1'=>'Content for panel 1',
'panel 2'=>'Content for panel 2',
'panel 3'=>$this->renderPartial('_content1',null,true),
),
'options'=>array(
'collapsible'=>true,
'active'=>1,
),
'htmlOptions'=>array(
'style'=>'500px;'
),
));
 
?>
 
 
 
通过定义panels 属性定义Accordion的几个可折叠的页面,通过配置 options传送参数给 JUI Accordion插件。
 
 
原文地址:https://www.cnblogs.com/youxin/p/3646331.html