Created
November 15, 2012 12:59
-
-
Save atmoz/4078529 to your computer and use it in GitHub Desktop.
Register assets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CustomModule extends CWebModule { | |
private $assetsUrl; | |
/** | |
* Publish assets by copying files to public assets folder, | |
* accessable by clients. | |
*/ | |
public function publishAssets() { | |
$this->assetsUrl = Yii::app()->getAssetManager()->publish( | |
Yii::getPathOfAlias($this->getId() . '.assets'), | |
false, | |
-1, | |
YII_DEBUG // Allways copy files when in debug mode | |
); | |
} | |
/** | |
* Register js and css files from assets | |
* | |
* @param string $file | |
*/ | |
public function registerFile($file) { | |
if (empty($this->assetsUrl)) { | |
$this->publishAssets(); | |
} | |
if (strpos($file, '.js') !== false) { | |
Yii::app()->clientScript->registerScriptFile($this->assetsUrl . '/js/' . $file, | |
CClientScript::POS_END); | |
} | |
else if (strpos($file, '.css') !== false) { | |
Yii::app()->clientScript->registerCssFile($this->assetsUrl . '/css/' . $file); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment