Home > Backend Development > PHP Tutorial > Create your own Widget instance in Yii, yiiwidget instance_PHP tutorial

Create your own Widget instance in Yii, yiiwidget instance_PHP tutorial

WBOY
Release: 2016-07-12 09:01:36
Original
878 people have browsed it

Create your own Widget instance in Yii, yiiwidget instance

The example in this article describes the implementation method of creating your own Widget in Yii. Share it with everyone for your reference, the details are as follows:

Here is a random advertising image as an example to illustrate the usage of Widget in Yii

1. Call Widget
Copy code The code is as follows:widget('WidgetName'); ?>
or

<&#63;php $widget=$this->beginWidget('path.to.WidgetClass'); &#63;>
...可能会由小物件获取的内容主体...
<&#63;php $this->endWidget(); &#63;>

Copy after login

You can also pass parameters to the Widget class

<&#63;php $userId = 1; &#63;>
<&#63;php $this->widget('WidgetName',array('userId'=>$userId)); &#63;>

Copy after login

The parameter userId is automatically mapped to the property of the same name of the Widget class, so don’t forget to declare this property when defining the Widget.

2. Create Widget

The custom Widget class must inherit CWidget and override the method run

<&#63;php
class BannerMagic extends CWidget {
  public function run(){
  }
}

Copy after login

or:

class MyWidget extends CWidget {
  public function init() {
    // 此方法会被 CController::beginWidget() 调用
  }
   public function run() {
    // 此方法会被 CController::endWidget() 调用
  }
}

Copy after login

The following is the implementation of BannerMagicWidget

<&#63;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,
   ));
  }
}

Copy after login

Store to protectedcomponentsBannerMagicWidget.php

The possible contents of the corresponding view file are as follows:
Copy code The code is as follows:whatever
Store to protectedcomponentsviewsbannermagic.php

3. Call the Widget
Copy code The code is as follows:widget('BannerMagicWidget'); ?>

I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.

Articles you may be interested in:

  • How to use widgets in yii
  • How to use yii, CI, yaf framework smarty templates
  • Yii usage tips Summary
  • Yii Quick Start Classic Tutorial
  • YiiFramework Getting Started Knowledge Points Summary (Picture and Text Tutorial)
  • Yii implements the method of inserting a comment form into the article details page of a single-user blog system
  • How YII uses the url component to beautify management
  • Yii’s method of removing asterisks in required fields

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1088773.htmlTechArticleCreate your own Widget instance in Yii, yiiwidget instance This article describes the implementation method of creating your own Widget in Yii. Share it with everyone for your reference, the details are as follows: Here is a random...
Related labels:
yii
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template