Tuesday, March 6, 2012

Insert widget to another widget in Yii

For this time I want to share how I accomplish inserting widget in yii to another widget..

You can take a look of this code of using widget in yii :

  $this->widget('ext.xxx.yyy',array(...)); 
It executing echo statement in yii so if you want to insert widget to another widget you have to get the result of this string.

To get this way you have to save the result before and then insert those string to another widget using ob_start(). 


Oke let's take a look of example how we can insert cjuislider inside of cdetailview..

the widget is as below :


<?php
//save the result of this result of string to slider variable
ob_start();
$this->widget('zii.widgets.jui.CJuiSliderInput', array(
    'name'=>'my_slider',
    'value'=>0,
    'event'=>'change',
    'options'=>array(
        'min'=>0,
        'max'=>100,
        'slide'=>'js:function(event,ui){$("#amount").text(ui.value);}',
),
    'htmlOptions'=>array(
        'style'=>'width:200px; float:left;'
),
));
echo '<div id="amount"></div>';
$slider=ob_get_contents();
ob_end_clean();

?>
<?php $this->widget('zii.widgets.CDetailView', array(
 'data'=>$model,
 'id'=>'hor-minimalist-b',
 'attributes'=>array(
  'project_id',
  'title',
  array(
         'label'=>'Project proposal',
         'type'=>'raw',
         'value'=>empty($model->project_proposal)?"":CHtml::link($model->project_proposal,Yii::app()->homeUrl.'/../upload/'.$model->project_proposal),
  ),
  array(
         'label'=>'Progress',
         'type'=>'raw',
         'value'=>$slider,//this variable that's get from the code above
  ),
  'start_time_actual',
  'end_time_actual',
//   'id_FK',
  'scope_name'
 ),
));
?>

Oke that's simple way how to insert widget to another widget in yii If you have another good solution just share yours..

6 comments: