3
votes

Yii is great but assets is the thing that was always strange for me (since the 1.1 version). I'm using Yii2 currently with Advanced app template. I want to register some css/js files in frontend main layout view (trying to use HTML5UP Prologue Template). How it can be done? I placed my css files under frontend/web/css directory, js under frontend/web/js and layout images under frontend/web/images dir.

Under frontend/assets directory I created PrologueAssets class like this:

namespace frontend\assets;
use yii\web\AssetBundle;

class PrologueAssets extends AssetBundle {

    public $sourcePath = "@webroot";
    public $css = [
        'css/skel.css',
        'css/style.css',
        'css/style-wide.css',
    ];
    public $js = [
        'js/jquery.min.js',
        'js/jquery.scrolly.min.js',
        'js/jquery.scrollzer.min.js',
        'js/skel.min.js',
        'js/skel-layers.min.js',
        'js/init.js',
    ];

    static function register($view) {
        die(self::sourcePath);
        parent::register($view);
    }

}

Unfortunately none of these files are registered. How to do that?

Little second question - how to register css files only if eg IE8 or IE9 is detected?

3
What happens when you include the CSS and JS files on AppAsset.php (instead of PrologueAssets.php? - Prabowo Murti
@PrabowoMurti - that was interesting. It tried to include the files but not in relation to base url but the current so it was working only on homepage. Pretty weird, ecpesially when at the same time the default yiis site.css is loaded properly. - Joe
I made a workaround - included the files in view using Url::base() to generate base url and by adding css/... or js/... to the end. I always thought assets are to complicated in Yii. - Joe

3 Answers

5
votes

You should simply try:

class PrologueAssets extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        ...
    ];
    public $js = [
        ...
    ];
}

http://www.yiiframework.com/doc-2.0/guide-structure-assets.html

PS : assets management is currently being refactored : https://github.com/yiisoft/yii2/pull/4855

1
votes

I wanted to add two files,one under frontend/web/css/custom.css and frontend/web/js/custom.js I just found the frontend/assets/AppAsset.php and added them directly and the two files are loaded for every page i load.

class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'css/site.css',
        'css/custom.css',
    ];
    public $js = [
        'js/custom.js',
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
}
0
votes

This is how you register js or css for ie 8 only

namespace app\assets;
use yii\web\AssetBundle;
class IE8Assets extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css=[
            'css/ie8.css'
    ];
    public $cssOptions = ['condition' => 'lte IE9'];
    public $js =[
            'js/jquery-1.11.3.min.js',
            'js/jquery-migrate-1.2.1.min.js',
    ]; 
    public $jsOptions = ['condition' => 'lte IE9'];
}