Dynamically adding one view inside other

Hi everybody,

Can anyone tell me how to insert one view dynamically(may be depending on the database value) inside another? I tried it using renderPartial() as follows:

public function actionMyView()


{


	$myViewObj = new MyViewForm();


	$showObj = new ShowForm();


	


	$output1 = $this->renderPartial('MyView',array('myViewObj'=>$myViewObj),true,false);


	


	$output2 = $this->renderPartial('show',array('showObj'=>$showObj),true,false);


	


	$layout=Yii::app()->layout;


	


	$layoutFile=$this->getLayoutFile($layout);


	


	$output=$this->renderFile($layoutFile,array('content1'=>$output1,'content2'=>$output2));





	$this->processOutput($output);


	


}

But it is giving following error:

session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cache limiter - headers already sent (output started at D:\xampp\htdocs\partialRenderDemo\protected\views\layouts\main.php:6)

I found the solution to this issue on google, but applying those solutions does not seem to work. So, can anyone tell may be other way to achieve the task of inserting one view inside other?

Thanks!  :)

When you call renderFile, you should set it as return true?

You call the render right inside the view.

The following code is from a view



  <?php /** Line:17-17*/ foreach($this->getMessageList($forum) as $message) {   ?>


  


    <div class="messageItem"><?php /** Line:19-22*/ $this->renderPartial('messageHead',array( "message"=>$message,"callback"=>'conversation',"linkClass"=>'messageListItem',"showStart"=>true)) ;    ?>


      


      <div class="messageContainer" style="display:none"></div>


    </div>                                                     


  <?php /** Line:26-26*/ }    ?>


My solution for an Ajax-replaceable list (table column or table-less) was to do this in the controller:



$output = $this->renderPartial('someListView',array_merge($this->lookupDb($id), array('someVar'=>$aVar)), true);


$this->render('content', array('someListDiv'=>$output)); // content layout


The lookup is expected to return a small amount of rows. I have a conditional div in someListView to be used on Ajax replacement.

/Tommy

Edit: Actually not "my solution", this is similar to what Yii does internally.

I also have a similar problem. I have a view that looks like this:

<div id="timeform">


  Here's a form to enter some time for a timetracker. It has a juidatepicker that onselect calls updateDayReport(d).


</div>





<div id="dayreport">


  <?php $this->actionReportDay($date); ?>


</div>





<script type="text/javascript">


/*<![CDATA[*/


function updateDayReport(d) {


    jQuery.ajax({


        'url': '<?php echo $this->createUrl('reportDay') ?>&date='+d,


        'cache':false,


        'success':function(html){ jQuery("#dayreport").html(html); }


    });


}


I want #dayreport filled on first page load. So i call the same action actionReportDay($date) that later will produce my AJAX response. That way i don’t need duplicate code to fetch the current time entries in the two actions.

<?php


    public function actionReportDay($date=null)


    {


        if ($date===null)


            $time=isset($_GET['date']) ? strtotime($_GET['date']) : null;


        else


            $time=strtotime($date);





        // Get time entries for current user


        $criteria=new CDbCriteria;


        // ... set up criteria and fetch data here





        $this->renderPartial('reportDay',array(


            'timeList'=>$timeList,


            'date'=>date('d.m.Y',$time)


        ));


    }


This works great so far. Questions are now:

  1. Is it o.k. to call a action from another view, if that action always uses renderPartial?

  2. And can i change the action's signature to accept an optional parameter like above?

Yes, I think your usage is fine.

Cool. Now if the core script problem with renderPartial() would be solved, i feel like Yii’s the perfect fit for any AJAX application i have in mind. :D

Yes, that problem is tricky. I'm still thinking of a way to solve it nicely. Will try to get it done by the release of 1.0.2. Maybe you can also help to think about it?