Widget CSS

After reading the guide and looking at the API, I'm still not exactly clear how to have a widget load it's own CSS file.

I see CClientScript has some 'register' functions, then I would call render() in some fashion?

I don’t get it ;)

Here's what I have.  The CSS file does not get loaded into the head.  I need some call in the view, right? But what it is?  I pieced this together from the guide's 'Creating Extensions' section:

I can see the CSS file placed into an Assets directory, so that part is working.

Might it be that

should be:


Ah, good catch, I corrected that.

I still don't see the CSS though.

Here's what works:

Maybe there's a better way.

Did you read the “Creating Extensions” section of the guide? I think it describes exactly what you want. E.g. you could use CWebApplication::assetManager to publish any CSS file that is bundled with your widget.

I read it and did manage to get the asset published but couldn't get the CSS to load.  Would you happen to have a working example with minimal code I could look at?  I also looked at the components/widgets included with the framework but couldn't get my own version to work.

I would like to get it done in the most Yii-ish way to avoid problems when deploying :)

Your example above should work. Lets put it together again (with some cleanup ;)):


class detailBar extends CWidget


    public $cssFile;


    public function run()




        if($this->cssFile===null) {

            // detailBar.css must be in the same directory as detailBar.php









Also make sure that in your Action that renders the view which uses your widget you call render() not renderPartial().

Works perfectly.  Thanks Mike  ;D