Yii Framework Forum: Layout variables - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Layout variables Inject variables to layout Rate Topic: -----

#1 User is offline   tuga 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 66
  • Joined: 02-January 10

Posted 02 January 2010 - 10:48 PM

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
0

#2 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

Posted 02 January 2010 - 11:25 PM

View Posttuga, on 02 January 2010 - 10:48 PM, said:

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.yiiframew...i/CClientScript
php:
foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n";

python:
[(animal, print(animal)) for animal in ['cat', 'dog', 'cow']]

ruby:
['cat', 'dog', 'cow'].each {|animal| puts animal}


You say Tomato, I say Tomato.
1

#3 User is offline   tuga 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 66
  • Joined: 02-January 10

Posted 03 January 2010 - 04:49 AM

View Postjayrulez, on 02 January 2010 - 11:25 PM, said:

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.yiiframew...i/CClientScript


Exactly what I was looking for.

Thank you jayrulez
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users