少编译yii2中的自定义Bootstrap CSS [英] Compiling custom Bootstrap css in yii2 from less

查看:187
本文介绍了少编译yii2中的自定义Bootstrap CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Yii2和Composer中启动一个新项目(此前与YII 1进行了一些项目).

I'm starting a new project in Yii2 and Composer (after a few projects with YII 1).

我用作曲家创建了一个新的高级项目".一切正常,但是,我想知道自定义引导主题的最佳方法是什么?

I created a new "advanced project" with composer. Everything is okay but, I'm wondering what is the best way to customize the bootstrap theme?

我认为将整个引导程序少复制到后端和前端,然后编辑两个变量文件并进行编译是不正确的方法.

I think copying the whole bootstrap less to the backend and to the frontend and edit the two variables file and compile it isn't the right way.

所以:是否可以扩展作曲家下载的较少文件并仅编辑两个变量文件?

So: is it somehow possible to extend the less files downloaded by the composer and edit only the two variables file?

推荐答案

引导程序配置可以通过更改variables.less来完成. 以下BootstrapAsset替代了原来的BootstrapAsset,它在当前目录中使用bootstrap-variables.less而不是原始variable.less.

Bootstrap configuration can be done by changing variables.less. The following BootstrapAsset is the replacement of original one, that uses bootstrap-variables.less in current directory instead of original variables.less.

namespace app\assets;

use yii\helpers\FileHelper;
use yii\web\AssetBundle;

class BootstrapAsset extends AssetBundle
{
    public $sourcePath = '@bower/bootstrap/dist';
    public $css = [
        'css/bootstrap.css',
    ];

    public function publish($am)
    {
        if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) {
            list ($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions);
        }

        $src = \Yii::getAlias($this->sourcePath);

        FileHelper::copyDirectory("$src/../less", $this->basePath."/less");

        $vars = file_get_contents(__DIR__ . "/bootstrap-variables.less");
        $css = \Yii::$app->cache->get("bootstrap-css-".crc32($vars));
        if (!$css)
        {
            file_put_contents("$src/../less/variables.less", $vars);

            ini_set('xdebug.max_nesting_level', 200);

            $less = new \lessc();
            $less->setFormatter(YII_DEBUG ? "lessjs" : "compressed");
            unlink($this->basePath . "/css/bootstrap.css");
            $less->compileFile("$src/../less/bootstrap.less", $this->basePath . "/css/bootstrap.css");
            \Yii::$app->cache->set("bootstrap-css-".crc32($vars), file_get_contents($this->basePath . "/css/bootstrap.css"));
        }
        else
        {
            file_put_contents($this->basePath . "/css/bootstrap.css", $css);
        }
    }
}

这篇关于少编译yii2中的自定义Bootstrap CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆