Hi,
I have several Layout files and View files. If we look at a single Layout and View, I have the following code:
MyLayout.php
Yii::app()->clientScript->registerScriptFile('script_file_1.js', CClientScript::POS_HEAD);
MyView.php
Yii::app()->clientScript->registerScriptFile('script_file_2.js', CClientScript::POS_HEAD);
Now, in the generated XHTML, ‘script_file_2.js’ appears in the HEAD section before ‘script_file_1.js’, where I require (and would expect) 1 to appear before 2.
This is a problem, as ‘script_file_1.js’ may include jQuery, and ‘script_file_2.js’ may use jQuery, but if file 2 appears before file 1, then errors are generated.
I know I can hard-code the ‘<script>’ tags within the Layout, but this is not a viable option for me, as I use various Themes, and libraries, depending on the Layout and View combinations. Also, I re-use Views, so I do not want to use the RegisterScriptFile instruction in the Controller action(s), as this would cause unnecessary duplication.
Does anyone know of a way around this issue? Am I totally misunderstanding how RegisterScriptFile should be used?
I have thought of extending CClientScript and adding extra POS_* properties, so that I can force the sequence in which scripts should be rendered over and above POS_HEAD, etc…, but it would be much nicer if there was some way to decide if a View or a Layout was issuing RegisterScriptFile and re-order accordingly. I’m not even sure if this issue becomes more complicated when considering Widgets, Modules, Extensions, etc…
Any advice greatly received.
Thanks,
Mike
btw… I’m using Yii 1.1.6