Create your own Widget instance and yiiwidget instance in Yii. Create your own Widget instance in Yii. yiiwidget instance this article describes how to create your own Widget in Yii. For your reference, here is an example of creating your own Widget and yiiwidget with Yii.
This article describes how to create a Widget in Yii. We will share this with you for your reference. The details are as follows:
Here, a random advertisement image is used as an example to describe the usage of widgets in Yii.
1. call widgets
The code is as follows: <? Php $ this-> widget ('widgetname');?>
Or
<? Php $ widget = $ this-> beginWidget ('path. to. widgetclass');?>... Body of content that may be obtained by small objects... <? Php $ this-> endWidget ();?>
You can also pass parameters to the Widget class.
<?php $userId = 1; ?><?php $this->widget('WidgetName',array('userId'=>$userId)); ?>
The userId parameter is automatically mapped to the same name property of the Widget class. Therefore, when defining a Widget, do not forget to declare this property.
2. create a Widget
The custom Widget class inherits the CWidget and overwrites the run method.
<?phpclass BannerMagic extends CWidget { public function run(){ }}
Or:
Class MyWidget extends CWidget {public function init () {// this method will be called by CController: beginWidget ()} public function run () {// this method will be called by CController :: endWidget () call }}
The following is the implementation of 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, )); }}
Stored in protected \ components \ BannerMagicWidget. php
The possible contents of the corresponding view file are as follows:
The code is as follows: "alt =" whatever "/>
Stored in protected \ components \ views \ bannermagic. php
3. call this Widget
The code is as follows: <? Php $ this-> widget ('bannermagicwidget ');?>
I hope this article will help you design PHP programs based on the Yii Framework.
Articles you may be interested in:
- Usage of widgets in yii
- Yii, CI, yaf framework + smarty template usage
- Yii tips
- Yii Quick Start Classic Tutorial
- YiiFramework knowledge point summary (graphic tutorial)
- Yii: how to insert a comment form to the document details page of a single-user blog system
- How to beautify YII by using url components
- Yii removes the star number in the required field
Examples in this article describes how to create a Widget in Yii. Share it with you for your reference. The details are as follows...