How To Render Two Views From Controller

I’ve got unusual simple requirement. I need to send content two 2 different view pages from my controller…

  1. themes/name/views/site/index.php

  2. themes/name/views/layouts/main.php

and I have a link which is pop up in this index.page. when user clicks this link in index.php… I need to show some content from themes/classic/views/layouts/main.php in a pop up.

so Now, I need to get the content into this main.php file. how can i send this content from the controller to main.php??

for themes/classic/views/site/index.php view i’m rendering index.php from siteController.php like this


$this->render('index',array(

				'item'=>$item,

				'term'=>$term,

				));

so for main.php page… do i need to render the main.php as well??

how can i get the data from the table to this main.php.

Many Thanks,

Dear Friend

I hope this is what you expect.

One simple implementation.

Controller




public function actionTrial(){

	 $index1="hello";

	 $index2="world";

	 $main="I am going to main land";


	 $this->render("//site/index",array(

	     'index1'=>$index1,

	     'index2'=>$index2,

	     "main"=>$main,

	 

	     ));

	 



views/site/index.php





<?php

echo $index1." ".$index2;

?>


<?php


$this->beginWidget('zii.widgets.jui.CJuiDialog', array(

'id'=>'mydialog',

'options'=>array(

'disabled'=>true,

'title'=>'Mainland',

'autoOpen'=>false,

'width'=>600,

'show'=>'fadein',

'modal'=>true,

    ),

));

?>

 

<?php 

     $this->renderPartial("//layouts/main",array('content'=>$main));//declare it 'content' since $content is inside the main.php

?>


<?php $this->endWidget('zii.widgets.jui.CJuiDialog');?>




<?php echo CHtml::link('Go to main land', '#', array(

   'onclick'=>'$("#mydialog").dialog("open"); return false;',

));

?>




I hope I helped a bit.

Regards.

Hi Developer much easier solution would be set a public data property in your controller and then you will have access in view and layout as well


example

<?php

class ControllerName ....

{

    $data;

    ...

    $this->data = array('item'=>$item, 'term'=>$term );

    $this->render('index');

    ...

} 



and then in your views you can access it by


$this->data['item'];

$this->data['term'];

Hi frn. thanks for your reply. i want to display in my layout as well… how to send data to layout file from same controller…like as we r doing for views file…

developer! read above what i said you will have access in your layout and views as well.

i want to send ddifferent data to layouts file and this data to views file…so i need to render it in twice…one in views and one in layouts

so… views data is different and layouts data is different.

With your answer and my first post… i am able to send data to views file

but i need to send some other data to themes/classic/views/layouts/main.php file

how can we do it…

using this… $this->render(‘index’);

i am able to get data in views/index file… but i need to get the data in themes/classic/views/layouts/main.php file as well…

this data will be different…

okay lets put it this way, in your sitecontroller you have index action


<?php

class SiteController extends CController

{

    public $data=array();


    public function actionIndex(){

        $this->data['viewData'] = 'view data';

        $this->data['layoutData'] = 'layout data';

        $this->render('index');


    }

} 

now in your view/index.php


<?php echo $this->data['viewData']; ?>

and in your layout file layouts/main.php


<?php if(!empty($this->data['layoutData']){ echo $this->data['layoutData']; } ?>

you can add whatever data you want to the data array or alternatively you can create a separate arrays for view and layout

EDITED

i treid ur approach but that is not working…we are sending data to index.php which is in views

$this->render(‘index’);

this is rendering index… i need to render layoutdata in main.php which is in layouts page…

themes/classic/views/layouts/main.php

so can i render in both views and layouts page like


$this->render('index');

  $this->render('main'); 

??

but its not happeing???

Dear Friends

@alirz23

The following code will throw error if other actions of same controller or other controller render the main layout file.




<?php echo $this->data['layoutData']; ?>



To circumvent that we have to do the following.




<?php if($this->id=='site' && $this->action->id=='index')

            echo $this->data['layoutData]; ?>



@developer!

Kindly check my solution. give your feedback.

Already $content variable available in main layout file.

Then we can pass the data as ‘content’ to main layout.

Regards.

JUST A EXAMPLE

simple check will sort that problem

@seenivasan look at your code that you suggested him to use its going to render that main layout twice.

Dear Friend

I have tested prior posting.

3717

dialog.png

Regards

exactly what i am talking about

sorry for the late reply guys… i could not come online again… though i tested it yesterday…huge thanks to both :) really you guys rocks :)

in layout.php




<?php if($this->id=='site' && $this->action->id=='index'){?>

           <?php  echo $this->data;}> 




controller





public $data;

...

...




//to send for layout page

$xyz =Model::model()->findByAttributes(array('name'=>'YII'));

foreach($xyz as $temp){

			$this->data = $temp;

		}




...

...

$this->render('index',array(

                                'item'=>$item,

                                'term'=>$term,

                                ));



Its working fine guys…Huge thanks to both again :)