How To Render Two Views From Controller

(developer!) #1

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 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





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,

(Chellamnivas) #2

Dear Friend

I hope this is what you expect.

One simple implementation.


public function actionTrial(){



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










echo $index1." ".$index2;



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














     $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.


(Alirz23) #3

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



class ControllerName ....




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




and then in your views you can access it by



(developer&#33;) #4

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…

(Alirz23) #5

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

(developer&#33;) #6

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…

(Alirz23) #7

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


class SiteController extends CController


    public $data=array();

    public function actionIndex(){

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

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




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


(developer&#33;) #8

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


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


so can i render in both views and layouts page like




but its not happeing???

(Chellamnivas) #9

Dear Friends


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]; ?>


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.


(Alirz23) #10


simple check will sort that problem

(Alirz23) #11

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

(Chellamnivas) #12

Dear Friend

I have tested prior posting.




(Alirz23) #13

exactly what i am talking about

(developer&#33;) #14

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;}> 


public $data;



//to send for layout page

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

foreach($xyz as $temp){

			$this->data = $temp;








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