Rizky Wijaya
Rizky Wijaya

Reputation: 17

I want to ask about custom widget in Yii2 advanced template

I have created a custom widget on my yii2 advanced project

I create new dir name components in backend dir after that, I create new dir again in components dir name views

in backend/components I create new PHP file name SideBWidget.php

<?php
namespace backend\components;

use yii\base\Widget;
use yii\helpers\Html;
use common\models\Content;

class SideBWidget extends Widget{

    public function run(){

        $models = Content::findAll([
            'c_pkey'=>0,
        ]);

        $this->render('sideb',[
            'model' => $models
        ]);
    }
}
?>

in backend/components/views I create sideb.php

<div id="sidebar-nav" class="sidebar">
        <div class="sidebar-scroll">
            <nav>
                <ul class="nav">
                    <?php foreach($model as $row): ?>
                      <li><a href="#" class=""><i class="lnr lnr-alarm"></i> <span><?php echo $row->c_name; ?></span></a></li>
                    <?php endforeach; ?>
                </ul>
            </nav>
        </div>
    </div>

and I call the widget in views/layout/main.php like this, and I also use the widget path use backend\components\SideBWidget;

<?= SideBWidget::widget() ?>

but when I run there is nothing and no error message. Where is the problem?

Upvotes: 0

Views: 448

Answers (1)

pa3py6aka
pa3py6aka

Reputation: 609

You must add return statement in run() function of widget:

return $this->render('sideb',[
    'model' => $models
]);

Upvotes: 1

Related Questions