[moka同学笔记]yii2.0小物件的简单使用(第二种方法)

1.在widgets/TestWidget.php中

 1 <?php
 2 /**
 3  * Created by PhpStorm.
 4  * User: moka同学
 5  * Date: 2016/08/05
 6  * Time: 18:15
 7  */
 8 namespace app\widgets;
 9 
10 use yii\base\Widget;
11 
12 class TestWidget extends Widget
13 {
14     public $msg = '';
15 
16     //初始化
17     public function init()
18     {
19         parent::init(); // TODO: Change the autogenerated stub
20     }
21 
22     //覆盖
23     public function run()
24     {
25         return $this->render('index',['msg'=>$this->msg]);
26     }
27 }

2.在widgets/views/index.php

1 <?php
2 /**
3  * Created by PhpStorm.
4  * User:moka同学
5  * Date: 2016/08/05
6  * Time: 18:20
7  */
8 echo $msg.'这个是公共部分';
9 ?>

3.在其他需要使用的视图文件中 如下调用即可

<?php
/**
 * Created by PhpStorm.
 * User: moka同学
 * Date: 2016/08/05
 * Time: 16:15
 */
use app\widgets\TestWidget;
?>
<?= TestWidget::widget();?>

注:~

转载请注明出处。QQ1727728211

posted @ 2016-08-05 18:47  mokal同学  阅读(191)  评论(0编辑  收藏  举报