sAe
(Admin)
June 9, 2009, 2:39am
1
Hello!
And once again i'm back with a question i've hit my head against.
How do i actually include css and js files specifically for just one view?
If i use client script inside a view my inclusions end up above the ones from the layout file. And if i do CHtml::cssFile(); i would just end up outputting it where i write it.
What am i missing here? How can i put them includes in the head part beneath the layout includes?
Thanks.
/Martin
sebas
(Sebathi)
June 9, 2009, 3:36am
2
Try this:
$file=dirname(FILE ).DIRECTORY_SEPARATOR.'file.css';
$url = Yii::app()->getAssetManager()->publish($file)
$cs->registerCssFile($url);
$js_file = dirname(FILE ).DIRECTORY_SEPARATOR.'file.js';
$js_url = Yii::app()->getAssetManager()->publish($js_file);
$cs->registerScriptFile($js_url);
And that's all.
rosko
(A)
June 9, 2009, 4:35am
3
Only one remark:
$cs = Yii::app()->getClientScript();
sAe
(Admin)
June 9, 2009, 4:50am
4
publish() is the magic function then i guess
What you are doing here is that you are including a css from the view folder, am i correct?
Thanks a lot!
/Martin
sAe
(Admin)
June 9, 2009, 5:04am
5
Actually, this didn't solve the problems, my other scripts keep getting beneath the ones i include in the view. Do i have to take the same procedure to my layout inclusions to be able to get them on top?
Currently im registering my scripts in my layout with something like…
// Get client script
$cs=Yii::app()->clientScript;
// Add CSS
$cs->registerCSSFile('/css/file.css');
// Add JS
$cs->registerScriptFile('/js/file.js', CClientScript::POS_HEAD);
sAe
(Admin)
June 9, 2009, 10:53pm
6
Anyone care to share how they solve this kind of issue?
Thanks!
/Martin
xx666xx
(Jerryablan)
June 9, 2009, 11:35pm
7
My suggestion is to create a layout specifically for this view. No fuss, no muss.
sAe
(Admin)
June 10, 2009, 2:58am
8
Quote
My suggestion is to create a layout specifically for this view. No fuss, no muss.
So you actually mean i should be copying html back and forth when i change something between layouts? That would defeat the purpose of having them.
qiang
(Qiang Xue)
June 10, 2009, 3:04am
9
@sAe : Use Yii::app()->clientScript->registerXyz() in your views to register the needed scripts. The registered scripts will be rendered right before the <title> tag. You can use this trick to control where the scripts in your layout should go.
xx666xx
(Jerryablan)
June 10, 2009, 3:43am
10
Agreed. Ideally, you'd load your CSS and JS files with the above method in your view. Hower, you said it hadn't worked. That was my backup plan.
jacmoe
(Jacob Moena)
December 23, 2010, 4:22pm
11
Are you using widgets here?
If you’re not, just add the files to your layout…
If using a theme:
<!-- blueprint CSS framework -->
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/reset.css" media="screen, projection" />
No need to worry about asset publishing if the files are already in a web accessible directory.
jacmoe
(Jacob Moena)
December 24, 2010, 2:14am
12
Very clever.
And typical of programmers to conjure up things like that.
Why use a simple approach when we can complicate it?
At the end of the day, I’d rather use this:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="en" />
<!-- blueprint CSS framework -->
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/screen.css" media="screen, projection" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/print.css" media="print" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/ie.css" media="screen, projection" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/main.css" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/form.css" />
<title><?php echo CHtml::encode($this->pageTitle); ?></title>
</head>
It works for me.
And it saves a lot of function calls.
But, each to his own.