0

The ListView widget is used to display data from a data provider. Each data model is rendered using the specified view file.I am using this function to pass data to the view.

A typical usage is as follows:

use yii\widgets\ListView;
use yii\data\ActiveDataProvider;

function actionGet_record() {
$dataProvider = new ActiveDataProvider([
   'query' => Post::find(),
   'pagination' => [
     'pageSize' => 20,
],
]);
echo ListView::widget([
   'dataProvider' => $dataProvider,
   'itemView' => '_post',
]);
}

The _post view file could contain the following:

<?php
use yii\helpers\Html;
use yii\helpers\HtmlPurifier;
?>
<div class="post">
<h2><?= Html::encode($model->title) ?></h2>

<?= HtmlPurifier::process($model->text) ?>    
</div>

Problem : Getting this error PHP Notice 'yii\base\ErrorException' with message 'Undefined variable: model'

How can I resolve it ?

Any help would be highly appreciated

alan54
  • 23
  • 4

1 Answers1

0

Try this

'itemView' => function ($model, $key, $index, $widget) { return $widget->render('_post', ['model' => $model]); }
StefanRado
  • 66
  • 3
  • 2
    This won't work as you are expecting. They are using the widget inside of controller so `$this->render()` will call the `render()` method of controller causing the view to be rendered with the layout. You should use `$widget->render()` instead. – Michal Hynčica May 22 '20 at 08:09