在 yii2 中从 less 编译自定义 Bootstrap css [英] Compiling custom Bootstrap css in yii2 from less
问题描述
我正在用 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.
那么:是否可以扩展由 Composer 下载的 less 文件并仅编辑两个变量文件?
So: is it somehow possible to extend the less files downloaded by the composer and edit only the two variables file?
推荐答案
Bootstrap 配置可以通过改变 variables.less 来完成.下面的 BootstrapAsset 是对原来的替换,使用当前目录下的 bootstrap-variables.less 而不是原来的 variables.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 appassets;
use yiihelpersFileHelper;
use yiiwebAssetBundle;
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 中从 less 编译自定义 Bootstrap css的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!