0

I want to combine a String and a Variable and Output it as a Variable.

Example:

@color-primary: #ff0000;

/* mixin call */
.create-button-color(primary);

/* mixin */
.create-button-color(@state) {

    .button-@{state} {
        @state-item: ~"@color-@{state}";

        background-color: @state-item;
        border-color: @state-item;
    }
}

Output:

.button-primary {
    background-color: @color-primary;
    border-color: @color-primary;
}

As you can see ~"@color-@{state}" outputs a String, but i need a Variable.
How i could do that?

robjke
  • 140
  • 6

1 Answers1

0

In Less, you can define a variable's name using another variable.

Documentation reference.

@color-primary: #ff0000;

/* mixin call */
.create-button-color(primary);

/* mixin */
.create-button-color(@state) {

    .button-@{state} {
        // Glue two parts of a variable: `color-` + `state`
        @state-item: "color-@{state}";
        // @state-item contains a string `color-primary`


        // Defile variable from string via @@
        background-color: @@state-item;
        border-color: @@state-item;
    }
}
3rdthemagical
  • 5,271
  • 18
  • 36