基于yii2的blog系统开发11-创建自定义小部件

接上一节:基于yii2的blog系统开发10-控制台命令程序的创建和执行:

第十八步:创建自定义小部件

1.在frontend/目录下新建文件夹components,在该文件夹下新建HelloWidget.php文件:

<?php
namespace app\components;
use yii\base\Widget;
use yii\helpers\Html;

class HelloWidget extends Widget
{
    public $mes;
    public function init()
    {
        parent::init();
        if($this->mes==null)
        {
            $this->mes = "hello";
        }
    }

    public function run()
    {
        return Html::encode($this->mes);
    }
}

2.在视图页面引用:

use frontend\components\HelloWidget;
<?= HelloWidget::widget(['mes'=>'hi,lq'])?>
发表评论?

0 条评论。

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>