使用LESSPHP覆盖Twitter的Bootstrap LESS变量

时间:2012-12-04 05:44:47

标签: php twitter-bootstrap less lessphp

我一直在讨论lessphpbootstrap-colorpicker,我想知道是否可以使用bootstrap-colorpicker设置的十六进制颜色覆盖较少的变量,例如:< / p>

<?php
if(isset($_POST['submit'])){
    require "lessc.inc.php";

    $less = new lessc;
    $less->setVariables(array("bodyBackground" => $_POST['color']));
    $less->checkedCompile("less/bootstrap.less", "output.css");

    try{
        $less->compile("invalid LESS } {");
    }catch(exception $e){
        echo "fatal error: " . $e->getMessage();
    }
}?>

标记:

<form method="post" action="">
    <input name="color" 
           type="text" 
           class="span3" 
           value="<?php if(isset($_POST['color'])){echo $_POST['color'];}else{echo       
                 '#8fff00';}?>" 
           id="cp1" />
    <input name="submit" class="btn btn-primary" type="submit" value="Submit"/>
</form>
<script>
$function(){
    var bodyStyle = $('body')[0].style;

    $('#cp1').colorpicker({format: 'hex'}).on('changeColor', function(ev){    
        bodyStyle.backgroundColor = ev.color.toHex();
});
</script>

我认为这会有效,但事实并非如此,我不完全确定setVariables将POST数据作为@bodyBackground: #new_hex;传递,因为我目前正在获取bootstrap的@white的默认值变量。我是Lessphp的新手,并且想知道在编译之前是否可以为引导较少的变量分配新值?

提前致谢

1 个答案:

答案 0 :(得分:0)

不,你不能(我想)。阅读http://leafo.net/lessphp/docs/#compiling上的文档。 $less->checkedCompile未使用您的设置$less->checkedCompile数组。导致checkedCompile()和compileFile()读取(较少)文件。

你能做什么: 1)添加如下行:`@import“_custom.less”;在less / bootstrap.less结束时。 2)你的PHP代码:

require "lessc.inc.php";

$fp = fopen('less/_custom.less','w');
fwrite($fp,'@bodyBackground : '.$_POST['color']."\n");
fclose($fp);

$less = new lessc;
//$less->setVariables(array("bodyBackground" => $_POST['color']));
$less->FileCompile("less/bootstrap.less", "output.css");

` 注意checkedCompile是非常基本的,它只检查输入文件的修改时间。它不知道来自@import的任何文件。

是的,你可以使用十六进制颜色。

更新如果您不想更改引导程序的文件,请尝试:

/* with twitter boostrap less files in ./bootstrap-master/less */
error_reporting(E_ALL);
ini_set('display_errors','on');
require('lessphp/lessc.inc.php');

file_put_contents('./custom.less','@bodyBackground : '.$_POST['color']."\n");

$less = new lessc;
$less->setImportDir(array('./bootstrap-master/less','.'));
try {
$css = $less->compile(file_get_contents('./bootstrap-master/less/bootstrap.less')."\n" . '@import "custom.less";'); 
} catch (Exception $ex) {
    echo "lessphp fatal error: ".$ex->getMessage();
}

file_put_contents('./bootstrap.css',$css);
exit;