Saturday, August 9, 2014

Yii - Calling Widget - PHPStorm Friendly



Old WAY
 
When I was just starting out. These code looks hideously cryptic and hard to understand .

  <?php $this->widget('zii.widgets.grid.CGridView', array(
/*'type'=>'striped bordered condensed',*/
'itemsCssClass'=>'table table-striped',
'dataProvider'=>$gridDataProvider,
'template'=>"{items}",
'columns'=>array(
array('name'=>'id', 'header'=>'#'),
array('name'=>'firstName', 'header'=>'First name'),
array('name'=>'lastName', 'header'=>'Last name'),
array('name'=>'language', 'header'=>'Language', 'type'=>'raw'),
array('name'=>'usage', 'header'=>'Usage', 'type'=>'raw'),

),
)); ?>

so I went experimenting on how to make these more readable and here is what I have found.


      $myGridview = new CGridView();
      $myGridview->itemsCssClass = 'table table-hover';
      $myGridview->dataProvider = $gridDataProvider;
      $myGridview->template = "{items}";
      $myGridview->columns = array(
          array('name'=>'id', 'header'=>'#'),
          array('name'=>'firstName', 'header'=>'First name'),
          array('name'=>'lastName', 'header'=>'Last name'),
          array('name'=>'language', 'header'=>'Language', 'type'=>'raw'),
          array('name'=>'usage', 'header'=>'Usage', 'type'=>'raw'),
      );
      $myGridview->init();
     echo $myGridview->run(); // to print the widget

yes the code looks longer than the first one , If you are using php storm as your IDE you can easily explore your widget's other attributes and functions.





No comments:

Post a Comment