如果定义了变量,则调用mixin

时间:2022-08-03 20:22:05

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

我尝试使用mixin guards with when condition,但在编译期间我收到以下错误:

.mixin is undefined

.mixin未定义

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

.mixin(#008000);

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

当我删除when条件时它工作。问题出在这里?


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.

一切正常。但是当我删除变量数组中的testColor变量时,它会崩溃,因为它没有定义。

1 个解决方案

#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)作为参数传递给mixin。尝试将以下行添加到Less文件的最顶部。

@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.

由于Less会延迟加载变量,如果在编译期间设置了一个值,则应该覆盖none。如果在编译期间没有设置变量,则上面的行仍然意味着该变量已定义并具有一些默认值。


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.

Less中没有直接的isnull或isnotnull类型的函数。但是,您可以通过为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.

在下面的示例中,我们将默认值指定为none,而不是颜色,因此当检查iscolor(@color)条件时,它将失败并且mixin不会产生任何输出。

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);
}

#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)作为参数传递给mixin。尝试将以下行添加到Less文件的最顶部。

@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.

由于Less会延迟加载变量,如果在编译期间设置了一个值,则应该覆盖none。如果在编译期间没有设置变量,则上面的行仍然意味着该变量已定义并具有一些默认值。


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.

Less中没有直接的isnull或isnotnull类型的函数。但是,您可以通过为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.

在下面的示例中,我们将默认值指定为none,而不是颜色,因此当检查iscolor(@color)条件时,它将失败并且mixin不会产生任何输出。

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);
}