Controller Behaviors With Render


I am trying to render a view from an attached behaviour to my controller. I have tried $this->render and $this->owner->render();

My proposed idea and issue is:

Have a print extension.

Have a action in my normal controller to get the data and partial render the header, footer and content.

Then I attach the behaviour, that enables me to pass that data and then the behaviour renders them ready for printing.

Controller code:

        public function actionPrint($id,$type = 'po'){

                $model = $this->loadModel($id);



                            1=> array(

                                'header' => $this->renderPartial('//purchaseOrder/print/viewHead',array(




                            2=> array(

                                'header' => $this->renderPartial('//purchaseOrder/print/viewHead',array(







Behavior code


class Printer extends CBehavior


   public function printer($data){





I am either going in totally the wrong direction or one of you guys will have the simplest answer (I hope!),

Thanks in advance.

you can’t attach a behavior to a controller.

@alirz23 : I’ve done it many times, without any problem. Behaviors can be attached to any subclass of CComponent, including CController.

@Dean Sanderson: Are you sure that printing requires a diiferent version of your page to be rendered? In most cases, CSS is sufficient enough.

I wanted to just render the page depending on how many times they wanted it to be printed, and have a central place to control the page breaks. Options to have the same header and footer but different content on each page, like invoices. I managed to do it with a widget In the end. Not sure it’s the best way. But managed to do it. I started widget before I started this approach but I couldn’t get the bootstrap CSS to work with bootstrap->register. But I found if I put bootstrap->register in my widget init it work.

I could be wrong But as far as I know its not documented in the docs I also tried it

It is possible to attache a behaviour, I did it. But I couldn’t call the render function. I Couldn’t call render in an attached action either.

You should read and post the error that occurs on creating the Printer component:

Missing argument 1 for Printer::printer(), …?

The solution:

The classname of the behavior should be another than the method you call: for example ‘PrinterBehavior’.

class PrinterBehavior extends CBehavior


    public function printer($data)


        $this->owner->render("index"); //better performance: $this->getOwner()->



class TestController extends Controller


    public function behaviors()


        return array(

            'printerBehavior' => array(

                'class' => 'PrinterBehavior'





   public function actionIndex()






Error was, printer and it’s attached behaviours does not have a method called render

Did you change the name of the behavior class?

Above example works for me, did you test it?

That could be the reason I will test again. I switched to the widget option but I will run this again to test

You are right! Finally. Must have been my naming of the behaviour. Was my first one I have tried. Is there anyway of using a layout and view file that is stored in my extension relative to the behaviour?

Don’t worry sorted it with:



Thanks every for your help!