Danny Valariola
Danny Valariola

Reputation: 1118

Yii including CSS and JS files

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

Upvotes: 3

Views: 6102

Answers (2)

Ivo Renkema
Ivo Renkema

Reputation: 2198

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.

Upvotes: 0

adamors
adamors

Reputation: 2656

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).

Upvotes: 5

Related Questions