Conditional conclude rules

Let's say I have the following:

@import 'hello.less'

      

Based on the value of the variable, I want to nest this inside another class. In pseudocode, I want to do the following:

if(@someVar = true):
  .someClass {
    @import 'hello.less'
  }
else:
  @import 'hello.less'

      

I'm guessing I can use the mixing guards, but I don't know how to handle the "else" case:

.someClass when (@someVar = true) {
  @import 'hello.less'
}

// Else?

      

+2


source to share


2 answers


Using not

and &

( &

expands to zero if its parent is global scope ( for example )):

@some-var: true;

.some-class when (@some-var) {
    @import (multiple) "hello";
}

& when not(@some-var) {
    @import (multiple) "hello";
}

      

Note that the import option is required in this case multiple

as both imports are always evaluated and the guards only control the content contained in the output or not.

---



Although in general I am wondering why you need to maintain code for different projects in the same file and control its compilation indirectly through variables. Instead, I suggest creating just a file with the names of the names:

.some-class {@import "hello";}

      

and import it unconditionally into the project you want this way.

+3


source


My current solution looks like this:

@someVar: true;

.someClass when (@someVar = true) {
  .main(false);
}

.main(@someVar);

.main(@x) when not (@x) {
  @import 'hello.less'
}

      



I'll mark this as an answer in case someone doesn't have a better one.

+1


source







All Articles