关于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

  1. <?php $this->widget('WidgetName'); ?>

或者

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

  1. <?php $userId = 1; ?>
  2. <?php $this->widget('WidgetName',array('userId'=>$userId)); ?>

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

2. 创建Widget 
自定义Widget类要继承CWidget,覆盖方法run

  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实现

  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. }

存储到protected\components\BannerMagicWidget.php

对应的view文件可能的内容如下:

  1. <img src="data:images/adverts/<?php echo $advert; ?>" alt="whatever" />

存储到protected\components\views\bannermagic.php

3. 调用该Widget

  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'=>'width: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'=>'width:500px;'
),
));
 
?>
 
 
 
通过定义panels 属性定义Accordion的几个可折叠的页面,通过配置 options传送参数给 JUI Accordion插件。
 
 

Yii widget使用的更多相关文章

  1. yii widget使用的3个用法

    yii视图中使用的widget方式总结:常用的有3种方式:一.显示详细信息: $this->widget('zii.widgets.CDetailView', array( 'data' =&g ...

  2. Yii使用笔记 2

    yii中的 getId等函数, id更多的是一个 string, 而不是数字. CCaptchaAction > CAction > CComponent. 实现是 IAction. yi ...

  3. yii2开发后记

    h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...

  4. [moka同学摘录]Yii2.0开发初学者必看

    想要了解更多YII,PHP方面内容,请关注本博客. 基础总结 1.修改默认控制器/方法 yii默认是site控制器,可以在web.php中设置$config中的'defaultRoute'='xxxx ...

  5. 转:Yii实战中8个必备常用的扩展,模块和widget

    转载自:http://www.yiiframework.com/wiki/180/yii8/ 在经过畅K网的实战后,总结一下在Yii的项目中会经常用到的组件和一些基本的使用方法,分享给大家,同时也给自 ...

  6. Yii实战中8个必备常用的扩展,模块和widget

    Yii实战中8个必备常用的扩展,模块和widget 在经过畅K网 的实战后,总结一下在Yii的项目中会经常用到的组件和一些基本的使用方法,分享给大家,同时也给自己留个备忘录,下面我以代码加图片说明. ...

  7. yii使用gii创建后台模块与widget使用

    yii使用gii创建后台模块与widget使用 1.在protected/config/main.php中打开gii的配置属性. 'gii'=>array( 'class'=>'syste ...

  8. Yii 通过widget小物件生成添加表单

    通过widget小物件创建添加商品的表单 视图里,表单以endWidget();?>结束 最终效果: 把表单提交过来的信息保存到数据库中去. 补充要点: 密码表单: <?php echo ...

  9. Yii中的CCheckBoxColumn在widget中的用法

    'columns'=>array(        array(            'class'=>'CCheckBoxColumn',            'id'=>'us ...

随机推荐

  1. HTML设置固定页脚飘浮

    Css /* 页脚 */.footSty{bottom: 0pt; margin: 0pt; position: fixed; width: 100%; z-index: 10 ! important ...

  2. HBase0.98.1 通过协调器进行表的行数统计

    1. 启用表aggregation,只对特定的表生效.通过HBase Shell 来实现 (1)disable指定表.hbase> disable 'student' (2)添加aggregat ...

  3. 微软企业库Microsoft Enterprise Library的相关文章链接

    微软企业库4.1学习笔记 http://blog.csdn.net/anyqu/article/category/1228691/3 黄聪:Enterprise Library 5.0 系列教程 ww ...

  4. Oracle (内连接)

    例如: 表xuesheng id name 1, Jack 2, Tom 3, Kity 4, nono 表kaoshi id grade 1, 56 2, 76 11, 89 内连接(显示两表匹配的 ...

  5. socket.io 实例

    //引用 var io = require('socket.io')(server);   //server io.on('connection', function(socket) {     // ...

  6. 十大算法---Adaboost

    当我们有针对同一数据集有多个不同的分类器模型时,怎样组合它们使预测分类的结果更加准确, 针对这种情况,机器学习通常两种策略. 1 一种是bagging,一种是boosting bagging:随机对样 ...

  7. 【BZOJ2752】【线段树】高速公路

    Description Y901高速公路是一条重要的交通纽带,政府部门建设初期的投入以及使用期间的养护费用都不低,因此政府在这条高速公路上设立了许多收费站. Y901高速公路是一条由N-1段路以及N个 ...

  8. linux 监控服务器脚本

    #!/bin/bash ctime=`date +%x%T`monitor_dir=/home/jk/if [ ! -d $monitor_dir ]; then    mkdir $monitor_ ...

  9. 修改原代码定制bootstrap

    1.下载对应的Bootstarap和node.js 注:less文件夹中包含了bootstrap中所有样式组件的less源代码: dist保存编译后的css和js等文件 2.命令行输入npm inst ...

  10. A Script Pro nginx URL重写规则无法播放MP4解决方法

    I am using nginx and I have already add the line location /file/ { rewrite ^/-]+)/([-]+)/([^/]*)/([- ...