Is there a way I can specify priority of CSS files. Consider this scenario.
I have included a standard text.css file in my layout/main.php using clientScript->registerCssFile, but in one of my controller I want to include controller specific CSS file(posts.css). I include it in controller’s view files (where required). Now what happens is that my posts.css is included first and then my text.css which overwrites rules defined in posts.css.
I have extended CClientScript to implement this requirement, as I use 960 framework and Yii includes it’s files after all other CSS files no matter where I register them.
May be this can help others.
<?php
class DX_ClientScript extends CClientScript
{
public function renderHead(&$output)
{
$html='';
foreach($this->metaTags as $meta)
$html.=CHtml::metaTag($meta['content'],null,null,$meta)."\n";
foreach($this->linkTags as $link)
$html.=CHtml::linkTag(null,null,null,null,$link)."\n";
/* changes by dx-sadaf */
foreach($this->cssFiles as $k=>$file){
$html.=CHtml::cssFile($file['url'],$file['media'])."\n";
}
/* end changes*/
foreach($this->css as $css)
$html.=CHtml::css($css[0],$css[1])."\n";
if($this->enableJavaScript)
{
if(isset($this->scriptFiles[self::POS_HEAD]))
{
foreach($this->scriptFiles[self::POS_HEAD] as $scriptFile)
$html.=CHtml::scriptFile($scriptFile)."\n";
}
if(isset($this->scripts[self::POS_HEAD]))
$html.=CHtml::script(implode("\n",$this->scripts[self::POS_HEAD]))."\n";
}
if($html!=='')
{
$count=0;
$output=preg_replace('/(<title\b[^>]*>|<\\/head\s*>)/is','<###head###>$1',$output,1,$count);
if($count)
$output=str_replace('<###head###>',$html,$output);
else
$output=$html.$output;
}
}
public function registerCssFile($url,$media='',$index=null)
{
$this->hasScripts=true;
if( !in_array(array('url' => $url, 'media'=> $media),$this->cssFiles))
{
if (is_null($index)) {
$this->cssFiles[] = array('url' => $url, 'media'=> $media);
}else{
array_splice($this->cssFiles,$index,null, array(array('url' => $url, 'media'=> $media)) );
}
}
$params=func_get_args();
$this->recordCachingAction('clientScript','registerCssFile',$params);
return $this;
}
public function isCssFileRegistered($url)
{
if( empty($this->cssFiles) ) {
return false;
}
foreach ($this->cssFiles as $k=>$file_info){
if ($file_info['url'] == $url) {
return true;
}
}
return false;
}
}