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 ...
随机推荐
- HTML设置固定页脚飘浮
Css /* 页脚 */.footSty{bottom: 0pt; margin: 0pt; position: fixed; width: 100%; z-index: 10 ! important ...
- HBase0.98.1 通过协调器进行表的行数统计
1. 启用表aggregation,只对特定的表生效.通过HBase Shell 来实现 (1)disable指定表.hbase> disable 'student' (2)添加aggregat ...
- 微软企业库Microsoft Enterprise Library的相关文章链接
微软企业库4.1学习笔记 http://blog.csdn.net/anyqu/article/category/1228691/3 黄聪:Enterprise Library 5.0 系列教程 ww ...
- Oracle (内连接)
例如: 表xuesheng id name 1, Jack 2, Tom 3, Kity 4, nono 表kaoshi id grade 1, 56 2, 76 11, 89 内连接(显示两表匹配的 ...
- socket.io 实例
//引用 var io = require('socket.io')(server); //server io.on('connection', function(socket) { // ...
- 十大算法---Adaboost
当我们有针对同一数据集有多个不同的分类器模型时,怎样组合它们使预测分类的结果更加准确, 针对这种情况,机器学习通常两种策略. 1 一种是bagging,一种是boosting bagging:随机对样 ...
- 【BZOJ2752】【线段树】高速公路
Description Y901高速公路是一条重要的交通纽带,政府部门建设初期的投入以及使用期间的养护费用都不低,因此政府在这条高速公路上设立了许多收费站. Y901高速公路是一条由N-1段路以及N个 ...
- linux 监控服务器脚本
#!/bin/bash ctime=`date +%x%T`monitor_dir=/home/jk/if [ ! -d $monitor_dir ]; then mkdir $monitor_ ...
- 修改原代码定制bootstrap
1.下载对应的Bootstarap和node.js 注:less文件夹中包含了bootstrap中所有样式组件的less源代码: dist保存编译后的css和js等文件 2.命令行输入npm inst ...
- A Script Pro nginx URL重写规则无法播放MP4解决方法
I am using nginx and I have already add the line location /file/ { rewrite ^/-]+)/([-]+)/([^/]*)/([- ...