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
时间: 2024-10-24 15:13:29