3

I've been reading on this quite some time...and i'm puzzled -

Can you help on what is the difference between:

Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl.'/css/some-file.css');

<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/some-file.css 

Is it a performance issue, or just different syntax?

Thanks, Danny

2 Answers 2

5

registerCssFile always registers the file between the <head> tags, even if you call it somewhere in a view. This is helpful if you care about HTML validation (a <link> in <body> is invalid), but still want to include a CSS file in a view.

registerCssFile actually aids performance, because the CSS is registered only when you want it (and need it).

Sign up to request clarification or add additional context in comments.

2 Comments

It should also be noted that registerCssFile has the advantage in that if you load the CSS in multiple places (e.g. in the view, in an extension, etc), it'll prevent it from being loaded multiple times . . .
you had better use an extended ClientScript component that combine and minify css and js like github.com/muayyad-alsadi/yii-EClientScript
0

The way you are using it, it is identical. To verify this, check the source of the page (in your browser) and check the statement that Yii::app()->clientScript->registerCssFile creates.

However, clientScript lets you control the position of the script in the HTML file. Check out: http://www.yiiframework.com/doc/api/1.1/CClientScript#registerScriptFile-detail and look for POS_HEAD, POS_BEGIN, POS_END.

What is probably more important is this: In the MVC philosophy, you want to have everything related to HTML-output in your view-file. Yii::app()->clientScript lets you add CSS and JS files from within your view files. And that is where you want it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.