简体   繁体   English

如果生成目标的语句较少

[英]LESS if statement for build targets

I'm looking for functionality in LESS to be able to change one variable and have it change a bunch of different variables so that I can change my compile target and have it easily change a ton of variables. 我正在寻找LESS中的功能,以便能够更改一个变量并使它更改许多不同的变量,以便我可以更改编译目标并使其轻松更改大量变量。

Here's an example of what I'm looking for. 这是我要寻找的示例。 The only problem is that that because I redefine @img inside .compile_for_if it's in another scope. 唯一的问题是,因为我在.compile_for_if重新定义了@img.compile_for_if它在另一个作用域中。

@compile_for: "endpoint1";

@img_endpoint1: "./myImg.png";
@img_endpoint2: "https://somewhere.com/myImg.png";
@img: "./default.png";
.compile_for_if
{
    & when (@compile_for = "endpoint1")
    {
        @img: @img_endpoint1;
    }
    & when (@compile_for = "endpoint2")
    {
        @img: @img_endpoint2;
    }
}

... somewhere else ...

.img
{
    background-image:url("@{img}");
}

You can achieve this by using enclosing all your rules within a nameless namespace ( &{...} ) and then calling your .compile_for_if mixin within its scope. 您可以通过将所有规则包含在无名命名空间( &{...} )中,然后在其范围内调用.compile_for_if mixin来实现此目的。 This would expose all variables set within the .compile_for_if into the namespace and thereby all rules will be able to access it. 这会将.compile_for_if设置的所有变量都暴露给名称空间,因此所有规则都将能够访问它。 ( Note: You would have to change your .compile_for_if definition also like given below.) 注意:您还必须更改.compile_for_if定义,如下所示。)

@compile_for: "endpoint1";

@img_endpoint1: "./myImg.png";
@img_endpoint2: "https://somewhere.com/myImg.png";
@img: "./default.png";

/* Added another set of conditional variables to illustrate */
@padding1: 10px;
@padding2: 20px;
@padding: 5px;

.compile_for_if when (@compile_for = "endpoint1"){
  @img: @img_endpoint1;
  @padding: @padding1;  

}
.compile_for_if when (@compile_for = "endpoint2"){
  @img: @img_endpoint2;
  @padding: @padding2;
}

&{
  .compile_for_if();
  .img{
    background-image:url("@{img}");
  }

  div{
    padding: @padding;
  }
}

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

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