Can I Put Cgridview In A Function?

I can use CGridView to show data now. But when I try to put it in a function, it doesn’t work.

The function declare is ok, but if I try to run it, it doesn’t show anything.

  function test() {

	$this->widget('zii.widgets.grid.CGridView', array(



  test(); // error

The reason I want to put it into a function is that I want to show 2 data models with same CGridView.

Is there any suggestion?

It’s technically possible to display CGridView in a function

  function test(CController $controller) {

	$controller->widget('zii.widgets.grid.CGridView', array(

   	// ...




but I don’t think it solves your problem.

What do you exactly mean by "2 data models"? Two related models or two models that share the same attributes (so they can fit into the same grid view structure)?

Thanks, it resolves my problem.

My requirement is, I want to "group" data. For example, I have 50 records data.

I don’t want to put them in 1 Grid and want to put them in 2 or more grids.

Therefore, I just want to put them into a function and call them by different criteria.

In action: generate your various $models. Pass all of them to the view file. attach to various GridViews as needed.