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

时间:2022-09-05 16:48:03

这是第一种方法,还有另一种方法,其实都差不多。

1.在创建widgets\HelloWiget.php

 <?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2016/08/05
* Time: 15:55
*/
namespace app\widgets; use yii\base\Widget; class HelloWidget extends Widget
{
public $msg = ''; //初始化
public function init()
{
parent::init(); // TODO: Change the autogenerated stub
echo '<ul>';
} //覆盖
public function run()
{
echo '</ul>';
} public function helloWidget($helloTest){
return '<li>'.$helloTest.'</li>';
} }

2.在视图中调用

说明:创建好widget后,可以在其他任意视图中进行调用

例如在views/test/index.php中

 <?php
/**
* Created by PhpStorm.
* User: moka同学
* Date: 2016/08/05
* Time: 16:15
*/
use app\widgets\HelloWidget; ?>
<?php $hello = HelloWidget::begin();?>
<?=$hello->helloWidget('hello1');?>
<?=$hello->helloWidget('hello2');?>
<?php HelloWidget::end();?>

注:~

转载请注明出处。QQ1727718211