标签:des style blog color io ar 使用 for sp
在控制器中(protected/controllers):
<?php class WidgetController extends Controller { public function actionIndex(){ $this->render(‘index‘); } }
在视图中(protected/index):
$this->widget(‘application.widgets.UserWidget‘,array( ‘num‘=>3 ));
自定义组件:
在protected/下创建widgets/UserWidget.php
class UserWidget extends CWidget { public $num; //自定義屬性 public function init(){ //判断是否传入参数 if(!$this->num){ $this->num = 5; } } //自定义运行方法 public function run(){ $users = $this->getUsers(); $this->render(‘users‘,array( ‘users‘=>$users )); } //方法执行体 protected function getUsers(){ $users = Yii::app()->db->createCommand()->select(‘id,name,create_time‘)->from("user")->order(‘create_time desc‘)->limit($this->num)->queryAll(); return $users; } }
在protected下创建widgets/views/users.php
<h1>自定義挂件的使用</h1> <?php if(!empty($users)) {?> <table border="0"> <tr> <th>用户id</th> <th>用户名</th> <th>创建时间</th> </tr> <?php foreach($users as $v) {?> <tr> <td><?php echo $v[‘id‘]?></td> <td><?php echo $v[‘name‘]?></td> <td><?php echo date("Y-m-d H:i",$v[‘create_time‘]);?></td> </tr> <?php } ?> </table> <?php } else {?> 没有查询到用户 <?php } ?>
标签:des style blog color io ar 使用 for sp
原文地址:http://www.cnblogs.com/liuwz/p/4047242.html