yii2.0實現創(chuàng)建簡單widgets示例

字號:


    本文實例講述了yii2.0實現創(chuàng)建簡單widgets的方法。分享給大家供大家參考,具體如下:
    namespace yii\widgets;
    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 = 'Hello World';
    }
    }
    public function run()
    {
    return Html::encode($this->message);
    }
    }
    調用方法:
    <?php
    use app\components\HelloWidget;
    ?>
    <?= HelloWidget::widget(['message' => 'Good morning']) ?>
    希望本文所述對大家基于Yii框架的PHP程序設計有所幫助。