简体   繁体   English

有没有一种方法可以用PHP中的LESS编译器覆盖LESS变量?

[英]Is there a way I can overwrite LESS variable with LESS compiler in PHP?

Currently I am using the lessphp as my LESS compiler. 目前,我正在使用lessphp作为我的LESS编译器。 I know it provides a way to set the LESS variables, but is there a way to overwrite the variables set in the file? 我知道它提供了一种设置LESS变量的方法,但是有没有一种方法可以覆盖文件中设置的变量?

For example, I have the following LESS file: 例如,我有以下LESS文件:

@theme-color: #000055;
h1 {
    color: @theme-color;
}
h2 {
    color: @theme-color * 1.3;
}
h3 {
    color: @theme-color * 1.5;
}

To compile it in PHP, 要在PHP中进行编译,

require"path/to/leafo/lessphp/lessc.inc.php";
$less = new lessc;
$less->setVariables(array(
    "theme-color" => "#000055"
));
$less->compileFile("basic/path/less/main.less", "basic/path//css/main.css");

It successfully compile the file but the variable @theme-color is not overwritten. 它成功编译了文件,但变量@ theme-color未被覆盖。 Is there a way to overwrite it? 有没有办法覆盖它?

I don't mind using another compiler if necessary, but it needs to be server side as the javascript compiler is too slow and it creates a moment of no css view. 我不介意在必要时使用其他编译器,但是它需要在服务器端使用,因为javascript编译器太慢并且创建了没有CSS视图的时刻。

Possible you can use compile() instead of compileFile() ; 可能您可以使用compile()而不是compileFile()

$lesscode = file_get_contents("basic/path/less/main.less");
$lesscode .= "@themecolor: #000055;";

file_put_contents("basic/path//css/main.css", $less->compile($lesscode));

Seems like there is no way to do that with leafo's lessphp compiler. 似乎没有办法用leafo的lessphp编译器来做到这一点 At last I have changed to use oyejorge's lessphp compiler. 最后,我更改为使用oyejorge的lessphp编译器。

With the oyejorge's lessphp compiler, the variable can be changed after loading the file and at the same time able to include file: 使用oyejorge的lessphp编译器,可以在加载文件后更改该变量,并且同时可以包含文件:

$parser = new Less_Parser();
$parser->parseFile( "path/to/less/main.less", url("path/to/less/") );
$parser->ModifyVars( array('theme-color'=>'#005500') );
$css = $parser->getCss();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM