How to renderPartial in Widget?

Hi,

I wrote my own widget.

Within the run-method I want to render a view.

How do I do it?




class myWidget extends CWidget 

{


	

    public function init()

    {

        ob_start();

    }

 

    public function run()

    {

        // do other Stuff


        // do render

        ??->renderPartial("viewXY");

    }

}



THX

Andres

$this->controller

/Tommy

The widget class will only have $this->render() method which will render a partial view (no layout) from your widgets/views folder.




// file: /widgets/views/my-widget.php


//widget class

$this->render('my-widget',array());



yeah thx… that worked…

Where do I have to put the views?

Can I use views from the regular view folder? If so, what is the path?

Thx andreas

This works even better…

Please be careful, in this way, if you call your widget from multiple controllers each of your controller must point to the same correct view file.

For example, if you have a post controller and you call your widget from within, the system will look for a view file located in /views/post/my-view-file.php

If you call it from a comment controller the system will look for a view located in /views/comment/my-view-file.php and of course it won’t find it and throw an error.

To overcome this issue, you can :




$this->controller->renderPartial('//post/my-view-file');



The above code will always use the “my-view-file.php” located in the post folder, doesn’t matter from which controller you call it.

Or you can remember that the widgets are self contained, therefore these should not have any controller dependencies, so you will place your view file, as i said before:




--components/widgets/MyWidget.php [class]

--components/widgets/views/my-view-file.php [view file]



And just call




$this->render('my-view-file');



from your wiget to fetch the file.

I would suggest using this approach, it is way cleaner than depending on a controller view file.

It solves my problem~ Thank you ~~