CClientScript positioning
Published on 6/29/2011
CClientScript is useful for including css files and javscript files, while avoiding duplication.
Things to know:
-
Yii injects css files just above the
tag. So, if you want to always override some included yii style, put your or AFTER the tag, and it will get loaded after the yii styles. -
I recommend putting all your
and in the and all your just before . are blocking, so your page will load faster if the are at the bottom. use:
Yii::app()->getClientScript()->coreScriptPosition = CClientScript::POS_END;
- if you want to include some inline javascript in a view, but make it load at the bottom, after say jquery, use the registerScript() method. To format your js nicely, you can use the Heredoc syntax:
/* load some formatted js into a php variable: */
$js = <<<EOF
var = ‘some javascript here!’;
function() { return ‘you can format it as you like, and include php $variables’; };
EOF;
/* write the script at the bottom of the document */
Yii::app()->getClientScript()->registerScript(“some id”, $js, CClientScript::POS_END);