YII: Publish and register CSS and JS on extension widget

The following are ways to publish and register CSS and JS at extension widget.

1. First Method: Widget JS contains multiple files

//Another way to publish widget JS folder with multiple files inside of it.
$assetsDir = dirname(__FILE__).DIRECTORY_SEPARATOR;
$baseUrl = Yii::app()->assetManager->publish($assetsDir.'/js');
Yii::app()->clientScript->registerScriptFile($baseUrl.'/FILE01.js');
Yii::app()->clientScript->registerScriptFile($baseUrl.'/FILE01.js');
Yii::app()->clientScript->registerScriptFile($baseUrl.'/FILE01.js');

2. Second Method: Place assets (JS, CSS, IMAGES etc) in one hash folder. But remember, all JS CSS etc should be placed in a folder.

$assetsDir = dirname(__FILE__).DIRECTORY_SEPARATOR;
$baseUrl = Yii::app()->assetManager->publish($assetsDir.'/assets');
Yii::app()->clientScript->registerCssFile($baseUrl.'/css/jquery-ui.css');
Yii::app()->clientScript->registerCssFile($baseUrl.'/css/style.css');
		
Yii::app()->clientScript->registerScriptFile($baseUrl.'/js/jquery-1.9.1.js');
Yii::app()->clientScript->registerScriptFile($baseUrl.'/js/jquery-ui.js');
Yii::app()->clientScript->registerScriptFile($baseUrl.'/js/autocomplete.js');

Tags:

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

You must be logged in to post a comment.