Yii Framework Forum: How To Render Two Views From Controller - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Render Two Views From Controller Rate Topic: -----

#1 User is offline   developer! 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 28-November 12

Posted 11 January 2013 - 11:35 AM

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

#2 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 11 January 2013 - 03:44 PM

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

#3 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 986
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 11 January 2013 - 10:35 PM

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

0

#4 User is offline   developer! 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 28-November 12

Posted 13 January 2013 - 12:28 PM

View Postalirz23, on 11 January 2013 - 10:35 PM, said:

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

#5 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 986
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 13 January 2013 - 12:30 PM

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

#6 User is offline   developer! 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 28-November 12

Posted 13 January 2013 - 12:35 PM

View Postalirz23, on 13 January 2013 - 12:30 PM, said:

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

#7 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 986
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 13 January 2013 - 01:08 PM

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
1

#8 User is offline   developer! 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 28-November 12

Posted 13 January 2013 - 01:29 PM

View Postalirz23, on 13 January 2013 - 01:08 PM, said:

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

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

#9 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 13 January 2013 - 01:30 PM

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

#10 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 986
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 13 January 2013 - 01:43 PM

Quote

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


JUST A EXAMPLE

simple check will sort that problem
1

#11 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 986
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 13 January 2013 - 02:11 PM

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

#12 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 13 January 2013 - 02:31 PM

Dear Friend

I have tested prior posting.

Attached File  dialog.png (36.12K)
Number of downloads: 14

Regards
1

#13 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 986
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 13 January 2013 - 02:34 PM

exactly what i am talking about
0

#14 User is offline   developer! 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 28-November 12

Posted 14 January 2013 - 05:43 AM

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 :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users