3

Kind of new to Yii. I'm trying to publish all files under the asset folder in a view.

Structure is like this :

/views/test

/views/test/assets

/views/test/assets/css

/views/test/assets/images

Now, what I did was publish the assets folder using Yii:app()->getAssetManager()->publish( dirname( __FILE__ ) . '/assets/' ); in the /views/test directory.

It publishes them all right, but now I have to call cs()->registerCssFile for all the css contained in /views/test/assets/css. How do I do that?

I know that the publish() method returns the path. Does this mean I have to do a foreach $file from the folder then call cs()->registerCssFile using the returned asset directory?

There may be an easier way, so any help is greatly appreciated! :)

2 Answers 2

4

I actually found the answer! While registerCssFile() does work, there's a much more elegant solution, Yii packages!

Consider the following file structure :

protected
\---views
   |---home
   |  \---assets
   |     |---css
   |     |  |---styles.css
   |     |  |---custom.css
   |     |  \---print.css
   |     |---js
   |     |  |---functions.js
   |     |  |---jquery.js
   |     |  \---ajax.js
   |     |---images
   |        |---img1.png
   |        |---imgn.png
   |        \---imgX.png
   \---index.php

You'd declare a package like so in index.php, taking care to use the Yii framework's dot notation for the basePath :

$package = array(
    'basePath'    => 'application.views.home.assets',
    'css'         => array( 'styles.css' ),
    'js'          => array( 'jquery.js', 'functions.js' )
);

Then, use Yii's clientScript to add and register the package.

Yii::app()->clientScript()
          ->addPackage( 'package-name', $package )
          ->registerPackage( 'package-name' );

What this will then do is publish ALL the contents of basePath and register the CSS and JS files specified in the package. For example, styles.css, custom.css, and print.css will all be published but only styles.css will be appended to the <head>.

Consider this rather than using :

$assetFolder = Yii::app()->assetManager->publish( dirname( __FILE__ ) . '/assets' );
Yii::app()->clientScript->registerCssFile( $assetFolder . 'styles.css' );
Yii::app()->clientScript->registerScriptFile( $assetFolder . 'jquery.js' );
Yii::app()->clientScript->registerScriptFile( $assetFolder . 'function.js' );

Michael is correct that there wouldn't be too many anyway so specifying the css and js files in the package declaration shouldn't be too much of a problem.

Sign up to request clarification or add additional context in comments.

2 Comments

Indeed. It also looks like Yii II is set up around packages of assets.
I had a problem with this
1

Your assumption is right: Yii does not look at the files in that directory. It only publishes the complete directory. It's your job to register all files that you need. You could open the directory from PHP and read in the directory content. But i'd avoid that on high-volume sites - it cost's precious CPU cycles.

I'd simply call registerCssFile() manually for each of those files. It shouldn't be too many anyway. And this way you have more control over which files are loaded.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.