Hi,
I created this class override, so that I could place JS files inside my application’s views wherever I wanted. All you need to do is setup this class in the components folder and enable it in the main.php config.
Inside your main view file, insert <!-- js --> where you want the JS scriptFiles to show up! I did this because I found the existing commands a bit limiting, especially when I am trying to stick to the HTML5 Boilerplate layout for my application.
Cheers,
Matt
class ClientScript extends CClientScript {
/**
* The script is rendered at the specified "replaced" HTML element
*/
const POS_REPLACE = 5;
public $coreScriptPosition=self::POS_REPLACE;
/**
* Inserts the scripts at the beginning of the body section.
* @param string $output the output to be inserted with scripts.
*/
public function renderReplace(&$output, $position='<!-- js -->')
{
$html='';
if(isset($this->scriptFiles[self::POS_REPLACE]))
{
foreach($this->scriptFiles[self::POS_REPLACE] as $scriptFile)
$html.=CHtml::scriptFile($scriptFile)."\n";
}
if(isset($this->scripts[self::POS_REPLACE]))
$html.=CHtml::script(implode("\n",$this->scripts[self::POS_REPLACE]))."\n";
if($html!=='') {
$count=0;
$output=preg_replace('/(<!-- js -->)/is','$1<###begin###>',$output,1,$count);
if($count) {
$output=str_replace('<###begin###>',$html,$output);
} else {
$output=$html.$output;
}
}
}
public function render(&$output)
{
if(!$this->hasScripts)
return;
$this->renderCoreScripts();
if(!empty($this->scriptMap))
$this->remapScripts();
$this->unifyScripts();
$this->renderHead($output);
if($this->enableJavaScript)
{
$this->renderBodyBegin($output);
$this->renderBodyEnd($output);
$this->renderReplace($output);
}
}
}