Reputation: 232
I use LessPHP (v0.3.4-2), read the documentation and search around this -basic- question but still not found the correct and straight answer.
Here is my .less file:
@bgcolor :#ccc;
.color(@color:#222) { color:@color; }
body {
margin:10px auto;
.color(@color);
background:@bgcolor;
}
My php code:
<?php
include 'lessc.inc.php';
$style_setting = array (
'color' => '#f00',
'bgcolor' => '#fee'
);
$cssFile = "stylesheet.css";
$lessFile = new lessc("less/stylesheet.less");
file_put_contents($cssFile, $lessFile->parse(null, $style_setting));
?>
Stylesheet result:
body {
margin:10px auto;
color:#ff0000;
background:#cccccc;
}
As you see on example above, the LesPHP arguments (.color(@color:#222)) is overwrited but the @bgcolor variable is not.
Just wondering is it correctly how LessPHP work or do I miss something here? Why does the LessPHP variable is not overwrited?
Any answer or hint is appreciated.
Upvotes: 1
Views: 648
Reputation: 5000
Why not have the same implementation of @bgcolor
as .color
?
If the following works for @color
:
.color(@color:#222) { color:@color; }
then the following should work for @bgcolor
:
.bgcolor(@bgcolor:#ccc) { background:@bgcolor; }
Update, an explanation of what is happening:
Let's assume this was all written in PHP, this is what a .color
function would look like:
function color($color = null) {
if(!$color) {
return 'color: #222;';
}
return 'color: $color;';
}
echo color(); //outputs "color: #222222;"
echo color('#f00'); //outputs "color: #ff0000;"
echo color(); //outputs "color: #222222;"
It takes the colour value passed OR outputs a default value. Now, this is what is happening with the @bgcolor
variable:
$bgcolor = '#fee';
$bgcolor = '#ccc';
echo 'color: '.$bgcolor.';'; //outputs "background: #cccccc;"
They key difference is that .color
is a function that can take values and @bgcolor
is a variable.
You've effectively defined what you want, instantly overwritten it and then called the variable. The only way to make it dynamic is to turn it into a 'function' like you have with color
.
Upvotes: 2