Layout variables

Hi everyone, this is my first post so please be patient.

I’ve been struggling with another framework, so I decided to try this one. I’m so glad I’ve decided to try this one, I have achieved more with this in a couple of days than with the other in 3 months. The documentation here is very good.

I’ve been reading a lot of tutorials and threads here in the forum, but I could not get a clear answer to this. But maybe my head is full of crap with the approach of other frameworks.

Can I inject some variables to the layout (template)? What I want to do is in some action of the controllers pass a variable to the layout.

Usually I use a separate css file for forms, but if I don’t use any forms I don’t need to put the link on the head of the document (layout). This could also apply to javascript files, maybe in one view we need some javascript files and in other we don’t.

So I tried on the action of the controller


public function actionIndex (){ 

$this->render('index', array('headCSS'=>CHtml::cssFile('/css/forms.css','screen')));

}

but $headCSS or $this->headCSS wasn’t available at the layout.

The only thing that is available here is the $content variable, the $data passed in the render method is not available.

It was very good in the head of the layout I could add something like


<head>

<?php echo $headCSS; ?> // or $this->headCSS

</head>

Am I missing anything??

I think I saw a solution for this with a widget. Is this the correct approach??

Thank you

You cannot pass variables to your layout. For what you want to do, i suggest you use CClientScript to accomplish it. Register the css/js files you want to use for specific actions in that action or view file.

eg you have an action register which will use your form.css file, you can include it by doing this:


Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl.'/css/form.css');

You may check the api page for CClientScript for more information

http://www.yiiframework.com/doc/api/CClientScript

Exactly what I was looking for.

Thank you jayrulez