Skip to content

Instantly share code, notes, and snippets.

@umidjons
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umidjons/9183820 to your computer and use it in GitHub Desktop.
Save umidjons/9183820 to your computer and use it in GitHub Desktop.
Publish assets (Bootstrap css, js, fonts) with CAssetManager in Yii
<?php
/**
* @var SiteController $this
* @var CClientScript $cs
* @var CAssetManager $am
*/
$cs = Yii::app()->clientScript;
$am = Yii::app()->assetManager;
$assetsDir = Yii::app()->basePath . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR;
$cs->registerCoreScript( "jquery" );
$cs->registerCssFile( $am->publish( $assetsDir . 'css' . DIRECTORY_SEPARATOR . 'bootstrap.min.css' ) );
$cs->registerCssFile( $am->publish( $assetsDir . 'css' . DIRECTORY_SEPARATOR . 'bootstrap-theme.min.css' ) ); // enable enhanced bootstrap theme
$cs->registerCssFile( $am->publish( $assetsDir . 'css' . DIRECTORY_SEPARATOR . 'styles.css' ) );
$cs->registerScriptFile( $am->publish( $assetsDir . 'js' . DIRECTORY_SEPARATOR . 'bootstrap.min.js' ), CClientScript::POS_END );
$ie8_resp_file_warn = $am->publish( $assetsDir . 'js' . DIRECTORY_SEPARATOR . 'ie8-responsive-file-warning.js' );
// publish fonts
$font_path = $assetsDir . 'fonts' . DIRECTORY_SEPARATOR; // /protected/assets/fonts/
$font_path_dest = $am->basePath . DIRECTORY_SEPARATOR . 'fonts' . DIRECTORY_SEPARATOR; // /assets/fonts/
$font_filename = 'glyphicons-halflings-regular';
$copy_needed = false;
foreach ( array( 'eot', 'svg', 'ttf', 'woff' ) as $f_ext )
if ( !file_exists( $font_filename . '.' . $f_ext ) )
{
$fp = $am->publish( $font_path . $font_filename . '.' . $f_ext );
$copy_needed = true;
}
if ( $copy_needed )
{
$font_path_src = Yii::getPathOfAlias( 'webroot' ) . dirname( $fp ); // /var/www/mysite/assets/b732073/
CFileHelper::copyDirectory( $font_path_src, $font_path_dest, array( 'level' => 0 ) );
}
?>
<html>
<!-- ... -->
<!--[if lt IE 9]>
<script src="<?= $ie8_resp_file_warn; ?>"></script>
<![endif]-->
<!-- ... -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment