Showing 2 Views On Same Page

Hi,

I’ve attempted to render two views on one page, using a range of potential options out there.

In this case, the two views come from different controllers.

I can see that in Yii 1, the main method to render two views was to use the renderPartial function. Apparently, Yii2 doesn’t use this, and that you can use the main render function off the controller, as suggested here:

at github.com/yiisoft/yii2/issues/1925

In my example, I am trying to include my ‘List Details’ view on my site/index view using the following code:





$searchModel = new frontend\models\Search\ListDetails();

$dataProvider = $searchModel->search(Yii::$app->request->queryParams);


echo $this->render('//list-details/index', ['dataProvider' => $dataProvider,'searchModel' => $searchModel]);






The code works, however the links are wrong. The create button references /site/create rather than /list-details/create.

I expect it is because I am calling $this from within the site controller, rather than creating a new controller for my list-details view.

What would be the right way to pull this off?

the method renderPartial is still available at yii2

http://www.yiiframework.com/doc-2.0/yii-base-controller.html#renderPartial()-detail

Thanks, I gave renderPartial a try also, with a similar config:


   

$searchModelListDetails = new frontend\models\Search\ListDetails();

$dataProviderListDetails = $searchModelListDetails->search(Yii::$app->request->queryParams);

echo Yii::$app->controller->renderPartial('//list-details/index', ['dataProvider' => $dataProviderListDetails,'searchModel' => $searchModelListDetails]);



I get the same issue, in that the code works, however the links are wrong. The create button references /site/create rather than /list-details/create.

Is there somewhere i can see an example implementation?

To be 100% specific, I am attempting to show my list-details/index view at the bottom of my site/index view

I suppose your problem is not the renderPartial, but rather the code which renders your create button to use the complete url with the right controller. I presume this button rendering code is in your index.php view file.

You must change this the create button code to something this:




echo Html::a('Create', ['/list-details/create'], ['class'=>'btn btn-success']);



The fix does work in this instance, but the gridview widget links are still askew. I did some hunting and believe two changes are required, being your fix, and:




GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

...

            ['class' => 'yii\grid\ActionColumn','controller' => 'list-details'],

        ],

]);



So, while i can get this to work, I am concerned that it’s more of a hack than it should be. I expect that much of these linking issues would not be a problem if I called the controller somehow within the site/index view.

Would this be a better idea?

I don’t see anything wrong with the explicitly set URLs and the approach you use, especially if it goes along with your project design. According to the docs, ‘controller’ option is provided exactly for the purposes you use it for.

There’s one more approach that might work, though I don’t think there’s any beauty in this approach anyway. So I’m giving a quick example only for a study purposes:

// TestController




public function actionIndex()

    {

        return $this->renderPartial('index');

    }



// SiteController actionIndex()




        $test = new TestController('test', $this->module);

	return $this->render('index', ['test'=>$test]);



// site/index.php





        <?php echo $test->actionIndex();?>



setting the controller property on Action Column is the right way to do it. you got it right


'columns'=>[

     ['class' => 'yii\grid\ActionColumn','controller' => 'list-details'],

],