如何使用yii2框架自定义widget
<?php namespace app\components; use yii\base\Widget; use yii\helpers\Html; class HelloWidget extends Widget{ public $message; public function init(){ parent::init(); if($this->message===null){ $this->message= ‘Welcome User‘; }else{ $this->message= ‘Welcome ‘.$this->message; } } public function run(){ return Html::encode($this->message); } } ?>
init() - should contain the widget properties, run() - should contain rendering result of the widget
<?php .......... class SiteController extends Controller { ........... public function actionCreatewidget(){ return $this->render(‘hellowidget‘); } ........... } ?>
<?php use app\components\HelloWidget; ?> <?= HelloWidget2::widget([‘message‘ => ‘ Yii2.0‘]) ?>
Welcome Yii2.0
原文:http://www.cnblogs.com/shaoyikai/p/5078345.html