Scripts and Stylesheet files in head tag when using Layout

I’m creating view files in a folder in themes folder and using the layout file main.php. Different pages have different javascript files and stylesheet included in the head tag. How do I work out with this ? Include those in the content file or layout file ? If I include it in the content file then it will appear inside the body tag of the resulting page because main.php has the header, menu and footer of the whole website

It’s really up to you. I’d say however, either:

  • put it in your view: you may then use CClientScript::POS_END (or else) to register your js files in the footer (after your other scripts referenced in protected/layouts/main.php), and POS_READY (or else) to register your js scripts in the $(document).ready() jQuery function

  • or put it in the main layout with some condition on controller and action ids, this could be interesting if these files are repeated through many views

Hi shaani

there is method that you can use in your content file that will put your css files in header automatically

<?php Yii::app()->clientScript->registerCssFile(‘path/to/the/file’); ?>

thanks