It there any way to create a "placeholder" inside the main view file for rendering other view files with specified data using Yii?
I want to individually process data in the controller then place them to a specified location in the view file before rendering.
Here is a widget example:
The widget class:
class MyWidget extends CWidget
{
public $someData;
public $mainData;
public function init()
{
}
public function run()
{
$this->render('mainView',array('data'=>$someData));
foreach($data as $dat)
{
if(dat["color"]=="red")
{
$display = 4;
}
else if(dat["color"]=="blue")
{
$display = 6;
}
etc....
//this is the fictional method for that purpose
$this->addToPage('mainView','subView','placeholderName',
array('display'=>$display,'mainData'=>$main));
}
}
}
The mainView file:
echo("<div class='someDesign'>");
echo($data);
$this->placeholder('placeholderName');
echo("</div>");
The subView file:
if($display>0 && $display<=4)
echo("<div class='dataColorG'>");
else if($display>0 && $display<=4)
echo("<div class='dataColorD'>");
echo $mainData;
echo("</div'>");
The solution based on Nikola's answer:
The widget class:
class MyWidget extends CWidget
{
public $someData;
public $mainData;
public function init()
{
}
public function run()
{
$output ="";
foreach($data as $dat)
{
if(dat["color"]=="red")
{
$display = 4;
}
else if(dat["color"]=="blue")
{
$display = 6;
}
//If it's a widget we need to use $this->controller->renderPartial() instead of $this->renderPartial()
$output.= $this->controller->renderPartial('subView',array('display'=>$display,'mainData'=>$main),true);
}
$this->render('mainView',array('subView'=>$output,'data'=>$someData));
}
}
The mainView file:
echo("<div class='someDesign'>");
echo($data);
echo($subView); //the 'placeholder'
echo("</div'>");
The subView file:
if($display>0 && $display<=4)
echo("<div class='dataColorG'>");
else if($display>0 && $display<=4)
echo("<div class='dataColorD'>");
echo $mainData;
echo("</div'>");