0

I try to use mixin guards with when condition, but during compiling I get the following error:

.mixin is undefined

.mixin (@color) when (isstring(@color)) {
  //some code
}

.mixin(#008000);

When I remove the when condition it works. Whats the problem here?


The problem is that we have variables inside our less-files which are defined in one compile-process. But during another compile-process this variables are not defined because we need a little bit dynamic here.

So I have to check if the variable is defined. When I try it over

$variables = [
   'testColor' => '#fff111',
];
$lessc->setVariables($variables);
$cachedCompile = $lessc->cachedCompile($publicPath .   $inputFile);

and

.mixin (@color:none) when (iscolor(@color)) {
   color: #fff;
}

.mixin(@testColor);

Everything works. But when I remove the testColor variable in the variable-array it crashes because it isn't defined.

Harry
  • 87,580
  • 25
  • 202
  • 214
user3314010
  • 41
  • 1
  • 4
  • There is nothing wrong with how you have defined the mixin and you should certainly not be getting that error. You can check on lesstester.com. Other than that, using `isstring()` on a color would mean that the mixin would not output anything because the condition fails. – Harry May 13 '15 at 14:25
  • You are also getting error with `when( @color = '')`? – Manwal May 13 '15 at 14:32
  • 1
    Yes, the main problem what i try to fix over a mixin guard is to check if a variable is defined or not. Is there another way to do this in less? – user3314010 May 13 '15 at 14:43

1 Answers1

1

Your problem could be because you are trying to pass a variable (@testColor) that does not exist as a parameter to the mixin. Try by adding the below line to the very top of your Less file.

@testColor: none;

Since Less does lazy loading of variables, if there is a value set during compilation that should overwrite the none. If no variable is set during compilation the above line would still mean that the variable is defined and has some default value.


There is no direct isnull or isnotnull type of function in Less. However, you can mimick the behavior by assigning a default value to the mixin.

In the below sample, we assign the default value as none which is not a color and hence when the iscolor(@color) condition is checked it would fail and the mixin would not produce any output.

I have also added a not condition below for you to see the difference.

.mixin (@color:none) when (iscolor(@color)) {
  //some code
  color: is defined;
}
.mixin (@color:none) when not (iscolor(@color)) {
  //some code
  color: is not defined;
}

#output1{
  .mixin();
}
#output2{
  .mixin(#ff0000);
}
#output3{
  .mixin(abcd);
}
Harry
  • 87,580
  • 25
  • 202
  • 214