YII自定义小部件
案例如下
common/widgets/TopMenu.php(地址可以自定义位置,命名空间一定要对应)
<?php /** * Created by PhpStorm. * Date: 2016/5/26 * Time: 18:09 * 头部尾部小部件 */ namespace common\widgets; use yii\base\Widget; class TopMenu extends Widget{ public function init(){ parent::init(); echo "<ul>"; } public function run(){ return "</ul>"; } public function addMenu($menuName){ return "<li>".$menuName."</li>"; } }
视图 menu.php
<?php /** * Created by PhpStorm. * Date: 2016/5/26 * Time: 18:15 */ use common\widgets\TopMenu; ?> <div> <?php $menu = TopMenu::begin();?> <?= $menu->addMenu('menu1');?> <?= $menu->addMenu('menu2');?> <?= $menu->addMenu('menu3');?> <?= $menu->addMenu('menu4');?> <?= $menu->addMenu('menu5');?> <?php TopMenu::end();?> </div>
控制器
public function actionMenu(){ return $this->renderPartial("menu"); }