Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bootstrap css asset is always loaded? #1263

Closed
basil-inc opened this issue Nov 21, 2013 · 20 comments
Closed

Bootstrap css asset is always loaded? #1263

basil-inc opened this issue Nov 21, 2013 · 20 comments
Assignees
Milestone

Comments

@basil-inc
Copy link
Contributor

I have my own CSS file, which I compile using less and which contains all the bootstrap code. Thus, I do not want to include the default bootstrap.css

I succeeded in registering my own CSS file in the \backend\assets\AppAsset.php file.

However, I also commented out the line that includes yii\bootstrap\BootstrapAsset as an item in the $depends array, and it keeps being included.

The only way I'm able to disable it, is by modifying the $css array in the yii-bootstrap vendor directory, but this is obviously not a good solution.

Any thoughts on this?

@qiangxue
Copy link
Member

Perhaps you used some bootstrap widget, such as NavBar in your layout?

@basil-inc
Copy link
Contributor Author

@qiangxue Yes, I do, but my own css contains code to style it. I'm worried about mobile performance, so I want to keep the number of HTTP requests and download size to a minimum.

It seems logical to me that the Bootstrap css should only be loaded if I include it in the AppAssets.

@cebe
Copy link
Member

cebe commented Nov 21, 2013

I have done the same, using bootstrap less sources. I just configured Assetmanager::bundles so that bootstrap asset does not load any case file.

@basil-inc
Copy link
Contributor Author

@cebe Can you show me an example of your code?

@qiangxue
Copy link
Member

@cebe you are faster. :)

Yes, just follow what @cebe suggested. The bootstrap css is injected because you used Bootstrap widgets.

@basil-inc
Copy link
Contributor Author

@qiangxue @cebe I assume you are talking about Yii::$app->assetManager->bundles or am I mistaken?
When I overwrite this array (with an empty array) in my config file (and trace it to check if it is truly empty), the asset is still loaded in the HTML header:

I'm probably forgetting something, but please paste an example of your solution. It will help not only me, but also anyone else searching for clues :-)

@qiangxue
Copy link
Member

try this:

'bundles' => [
    'yii\bootstrap\BootstrapAsset' => [
       'css' => [],
    ]
]

@basil-inc
Copy link
Contributor Author

@qiangxue thank you, you guys are wonderful.

@makroxyz
Copy link
Contributor

@qiangxue
I did it in my backend/config/main.php file and works fine.
My AppAsset loads my bootstrap.css and renderAjax works fine too.

But when I call gii module bootstrap css is missing because of the above config
Is there a simple way to solve?

@cebe
Copy link
Member

cebe commented Feb 25, 2014

You may override bootstrap asset in your AppAsset::init() to have the changes only applied when your asset is loaded.

@cebe
Copy link
Member

cebe commented Feb 25, 2014

like this:

class AppAsset extends AssetBundle
{
    // ...
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];

    public function init()
    {
        parent::init();
        // resetting BootstrapAsset to not load own css files
        Yii::$app->assetManager->bundles['yii\\bootstrap\\BootstrapAsset'] = [
            'css' => []
        ];
    }
}

@makroxyz
Copy link
Contributor

@cebe
I've already tried that way. But I have a little problem.
I have an extension that provides bootstrap Modal and an Action. When button is clicked it opens the Modal and fires a jquery load calling a route to Action (configured in actions() in my app controller)... Action returns the modal-body by renderAjax call.
With your suggesion the action reloads bootstrap.css

@cebe
Copy link
Member

cebe commented Feb 25, 2014

Yeah, right... need to check whether we can improve this somehow...

@cebe cebe reopened this Feb 25, 2014
@cebe cebe self-assigned this Feb 25, 2014
@makroxyz
Copy link
Contributor

Thank you @cebe

@qiangxue
Copy link
Member

It seems gii and debug modules should reset AssetManager::bundles.

@cebe
Copy link
Member

cebe commented Feb 25, 2014

agree.

@makroxyz
Copy link
Contributor

makroxyz commented Mar 6, 2014

I did another try.

'assetManager' => [
            'bundles' => [
                'yii\bootstrap\BootstrapAsset' => [
                    'basePath' => '@webroot',
                    'baseUrl' => '@web',
                    'css' => ['css/bootstrap.css']
                ],
            ],
        ],

now it always loads my css bootstrap, gii too.
renderAjax also reloads this css, but I have an issue since I have a custom.css file which modifies bootstrap settings a bit. When renderAjax reloads bootstrap.css it replaces all styles on page without (of course) custom.css...
Is it possibile to tell renderAjax to avoid to register an AssetBundle?

@mikehaertl
Copy link
Contributor

I don't think this is fixed. I have the same problem as #974. I want to use my custom Bootstrap built from the LESS sources, so I did as suggested above:

'bundles' => [
    'yii\bootstrap\BootstrapAsset' => [
       'css' => [],
    ]
]

Now the debug toolbar is broken as the yii\bootstrap\BootstrapAssets are blocked.

In my main layout I register the AppAsset which looks like this:

namespace app\assets;

use yii\web\AssetBundle;

class AppAsset extends AssetBundle
{
    public $depends = [
        'app\assets\BootstrapAsset',
        'yii\bootstrap\BootstrapPluginAsset',
    ];
}

And my custom BootstrapAsset is:

namespace app\assets;

use yii\web\AssetBundle;

class BootstrapAsset extends AssetBundle
{
    public $sourcePath = '@app/less';
    public $css = [
        'application/main.less',
    ];
}

As a workaround I've changed the namespace of my BootstrapAsset to yii\bootstrap and added this to my index.php:

Yii::$classMap['yii\bootstrap\BootstrapAsset'] = realpath(__DIR__.'/../assets/BootstrapAsset.php');

@cebe
Copy link
Member

cebe commented Apr 22, 2014

I don't think this is fixed.

the issues is still open ;)

@mikehaertl
Copy link
Contributor

the issues is still open ;)

Oh, right, sorry. I was mislead by the link to #974.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants