简体   繁体   English

LESS-如何在混合/循环中解析/转义参数变量(即变量变量)

[英]LESS - how to parse/escape parametric variables (ie. variable variables) in a mixin/loop

I'm attempting to loop through a number of indexed variables, in this case colours, to create a class for each. 我试图遍历许多索引变量(在本例中为颜色)以为每个变量创建一个类。 Something along the lines of this: 与此类似:

@colour-1: #FF0000;
@colour-name-1: "red";

.loop (@index) when (@index > 0) {
  @colour: ~"@{colour-@{index}}";
  @name: "@{colour-name-@{index}}";

  (~'*[data-colour="@{name}"]') {
    background-color: @colour;
    background-color: hsla(hue(@colour), saturation(@colour), lightness(@colour), 0.5);
  }

  .loop(@index - 1);
}
.loop (0) {}
.loop (1);

Rather than providing a fallback variable for IE<9, I'd like to update each colour/value programatically within the mixin to provide both an RGBA & Hex version. 与其提供IE <9的后备变量,不如以编程方式在mixin中更新每种颜色/值,以提供RGBA和Hex版本。 Problem is the @{@var} doesn't evaluate until after the fact and so won't parse as a colour. 问题是@ {@ var}直到事后才求值,因此不会解析为颜色。

Is there a way to escape the variables so they don't pass by reference? 有没有一种方法可以对变量进行转义,以使它们不被引用传递? JSFiddle here showing the output: http://jsfiddle.net/Qj2cZ/ JSFiddle在这里显示输出: http : //jsfiddle.net/Qj2cZ/

The multiple reference to the variable is definitely causing issues with the color functions. 对该变量的多重引用肯定会导致颜色函数出现问题。 This may be a bug. 这可能是一个错误。 I have not come up with a solution for LESS 1.3.3 or lower. 我还没有想出LESS 1.3.3或更低版本的解决方案。

However, I did come up with a working solution in the latest (currently beta) version (1.4) of LESS by building two larger "array" type variables, @colours and @colour-names to put all the individually defined color variables into. 但是,我确实通过构建两个更大的“数组”类型变量@colours@colour-names来放置所有单独定义的颜色变量, @colours在LESS的最新(当前beta)版本(1.4)中提出了一个@colours解决方案。 Then we use the new extract() function to access those in the loop, and you can get what you desire. 然后,我们使用新的extract()函数访问循环中的那些函数,您可以获得所需的内容。 Whether 1.4 is an option for you or not at this time you will have to determine. 您现在必须确定是否选择1.4。

LESS 1.4 Working 更少1.4工作

LESS Code LESS代码

@num-colours: 3;

@colour-1: #FF0000;
@colour-name-1: "red";
@colour-2: #00FF00;
@colour-name-2: "green";
@colour-3: #0000FF;
@colour-name-3: "blue";

@colours: @colour-1 @colour-2 @colour-3;
@colour-names: @colour-name-1 @colour-name-2 @colour-name-3;

.define-colours-loop (@index) when (@index =< @num-colours) {
  @colour: extract(@colours, @index);
  @name: extract(@colour-names, @index);

  *[data-colour="@{name}"] {
    background-color: @colour;
    background-color: hsla(hue(@colour), saturation(@colour), lightness(@colour), 0.5);
  }

  .define-colours-loop((@index + 1));
}
.define-colours-loop (0) {}
.define-colours-loop (1);

CSS Output CSS输出

*[data-colour="red"] {
  background-color: #ff0000;
  background-color: rgba(255, 0, 0, 0.5);
}
*[data-colour="green"] {
  background-color: #00ff00;
  background-color: rgba(0, 255, 0, 0.5);
}
*[data-colour="blue"] {
  background-color: #0000ff;
  background-color: rgba(0, 0, 255, 0.5);
}

On further examination (and following a bit of a breather) I found a working solution for less v1.3.3. 经过进一步检查(稍作喘息),我发现了适用于v1.3.3的可行解决方案。 If you are using v1.4 I would still recommend ScottS's answer, as this hacks around a bug. 如果您使用的是v1.4,我仍然会推荐ScottS的答案,因为这会绕过一个bug。

As mentioned, the issue is with the variable not being parsed in time for the color() function. 如前所述,问题在于没有及时为color()函数解析变量。 However if the variable is evaluated in one function and passed to another for conversion to a colour everything works fine. 但是,如果将变量在一个函数中求值并传递给另一个函数以转换为颜色,则一切正常。 So the following outputs the required styles: 因此,以下输出所需的样式:

@colour-1: #FF0000;
@colour-name-1: "red";

.loop (@index) when (@index > 0) {
  @colour: ~"@{colour-@{index}}";
  @name: "@{colour-name-@{index}}";
  .setColour(@colour, @name);
  .loop(@index - 1);
}

.setColour (@colour-string, @name) {
  @colour: color(@colour-string);
  (~'*[data-colour="@{name}"]') {
    background-color: @colour;
    background-color: hsla(hue(@colour), saturation(@colour), lightness(@colour), 0.5);
  }
}

.loop (0) {}
.loop (1);

JSFiddle here: http://jsfiddle.net/LJ3zX/ JSFiddle在这里: http : //jsfiddle.net/LJ3zX/

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

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