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 $this->widget('WidgetName'); ?>
或者
- <?php $widget=$this->beginWidget('path.to.WidgetClass'); ?>
- ...可能会由小物件获取的内容主体...
- <?php $this->endWidget(); ?>
- <?php $userId = 1; ?>
- <?php $this->widget('WidgetName',array('userId'=>$userId)); ?>
参数userId自动映射到Widget类的同名属性,所以在定义Widget时,别忘记了声明该属性。
2. 创建Widget
自定义Widget类要继承CWidget,覆盖方法run
- <?php
- class BannerMagic extends CWidget {
- public function run(){
- }
- }
或者:
- class MyWidget extends CWidget {
- public function init() {
- // 此方法会被 CController::beginWidget() 调用
- }
- public function run() {
- // 此方法会被 CController::endWidget() 调用
- }
- }
下面是是BannerMagicWidget实现
- <?php class BannerMagicWidget extends CWidget {
- public function run() {
- $random = rand(1,3);
- if ($random == 1) {
- $advert = "advert1.jpg";
- } else if ($random == 2) {
- $advert = "advert2.jpg";
- } else {
- $advert = "advert3.jpg";
- }
- $this->render('bannermagic',array(
- "advert"=>$advert,
- ));
- }
- }
存储到protected\components\BannerMagicWidget.php
对应的view文件可能的内容如下:
- <img src="data:images/adverts/<?php echo $advert; ?>" alt="whatever" />
存储到protected\components\views\bannermagic.php
3. 调用该Widget
- <?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 ( ))
Yii widget使用的更多相关文章
- yii widget使用的3个用法
yii视图中使用的widget方式总结:常用的有3种方式:一.显示详细信息: $this->widget('zii.widgets.CDetailView', array( 'data' =&g ...
- Yii使用笔记 2
yii中的 getId等函数, id更多的是一个 string, 而不是数字. CCaptchaAction > CAction > CComponent. 实现是 IAction. yi ...
- yii2开发后记
h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...
- [moka同学摘录]Yii2.0开发初学者必看
想要了解更多YII,PHP方面内容,请关注本博客. 基础总结 1.修改默认控制器/方法 yii默认是site控制器,可以在web.php中设置$config中的'defaultRoute'='xxxx ...
- 转:Yii实战中8个必备常用的扩展,模块和widget
转载自:http://www.yiiframework.com/wiki/180/yii8/ 在经过畅K网的实战后,总结一下在Yii的项目中会经常用到的组件和一些基本的使用方法,分享给大家,同时也给自 ...
- Yii实战中8个必备常用的扩展,模块和widget
Yii实战中8个必备常用的扩展,模块和widget 在经过畅K网 的实战后,总结一下在Yii的项目中会经常用到的组件和一些基本的使用方法,分享给大家,同时也给自己留个备忘录,下面我以代码加图片说明. ...
- yii使用gii创建后台模块与widget使用
yii使用gii创建后台模块与widget使用 1.在protected/config/main.php中打开gii的配置属性. 'gii'=>array( 'class'=>'syste ...
- Yii 通过widget小物件生成添加表单
通过widget小物件创建添加商品的表单 视图里,表单以endWidget();?>结束 最终效果: 把表单提交过来的信息保存到数据库中去. 补充要点: 密码表单: <?php echo ...
- Yii中的CCheckBoxColumn在widget中的用法
'columns'=>array( array( 'class'=>'CCheckBoxColumn', 'id'=>'us ...
随机推荐
- LINUX 性能 测试 优化工具
监控 测试 优化
- JAVA中的static修饰的方法不能被重写
可以重写静态方法,但重写后的静态方法不支持多态. 其实static根本就没有重写之说.static方法引用的时候应该用类名来引用,而不是对象.同时static方法不参与继承,所以在继承体系里面也不存在 ...
- reactjs 入门
地址搜集:http://www.cocoachina.com/webapp/20150604/12035.html class 属性需要写成 className ,for 属性需要写成 htmlFor ...
- C# - Sql数据类型的对应关系
<Language From="SQL" To="C#"> <Type From="bigint" To="lo ...
- SSL VPN 详解
SSL VPN是专栏VPN系列技术原理的最后一篇,SSL VPN作为远程接入型的VPN,已经具备非常广阔的前景,它的主要适应场景是取代L2TP Over IPSec,但功能要比L2TP Over IP ...
- 【转】WF事件驱动
转自:http://www.cnblogs.com/Mayvar/category/315963.html 这系统的教程有代码可以下载 WF事件驱动(5) 摘要: 之前,我通过4篇文章介绍了在WF4中 ...
- POJ2584 T-Shirt Gumbo 二分图匹配(网络流)
#include <cstdio> #include <cstring> #include <algorithm> const int inf=0x3f3f3f3f ...
- SGU 190.Dominoes(二分图匹配)
时间限制:0.25s 空间限制:4M 题意: 给定一个N*N的棋盘,一些格子被移除,在棋盘上放置一些1*2的骨牌,判定能否放满,并且输出任意方案. Solution: 首先考虑对棋盘的一个格子黑白染色 ...
- SGU 194. Reactor Cooling(无源汇有上下界的网络流)
时间限制:0.5s 空间限制:6M 题意: 显然就是求一个无源汇有上下界的网络流的可行流的问题 Solution: 没什么好说的,直接判定可行流,输出就好了 code /* 无汇源有上下界的网络流 * ...
- PHP: 使用CURL访问FTP
今天要做FTP上传.本想用PHP自带的FTP函数来实现,结果发现这个模块没有编译进来,重新编译PHP太麻烦,改用其他方式实现吧 FTP上传 if (isset($_POST['Submit'])) ...