Skip to content

Indirect mixin calls / Mixin references #1338

Closed

Description

@glasserc

Hi, I didn't see this in the open issues, so forgive me if it's called something else and I didn't see it..

At work we have a standard set of "colors" that we'd like to use in a lot of places. We'd like to define a mixin that we can use to repeat mixins for each color. An example of what we'd like:

.forManyColors(@mixinName) {
  .@{mixinName}("red");
  .@{mixinName}("blue");
  .@{mixinName}("green");
  .@{mixinName}("purple");
}

.background(@color){
  .background-@{color} {
    background-image: url("my-background-@{color}.jpg");
  }
}

.forManyColors("background");

Ideally, this should compile to:

.background-red {
    background-image: url("my-background-red.jpg");
} 
.background-blue {
    background-image: url("my-background-blue.jpg");
}
.background-green {
    background-image: url("my-background-green.jpg");
}
.background-purple {
    background-image: url("my-background-purple.jpg");
}

Unfortunately, it seems like interpolation in mixin names isn't possible. The code above gives me:

ParseError: Syntax Error on line 2 in /home/ethan/tmp/foo.less:2:18
1 .forManyNumbers(@mixinName) {
2   .@{mixinName}(4);
3   .@{mixinName}(3);

Here's another example:

.mixin-1 {
  width: 100px;
}

.mixin-2 {
  background-color: red;
}

@mixinNumber: 1;

.some-element {
  .mixin-@{mixinNumber};
}

Activity

  1. lukeapage commented on May 28, 2013

    @lukeapage
    Member

    duplicate of #1196

  2. glasserc commented on May 28, 2013

    @glasserc
    Author

    Although the names are similar, #1196 is about defining mixins using an interpolated name, and this one is about invoking mixins using an interpolated name. I suspect that the two issues will be resolved in different ways. If you'd like to merge the two, I can comment with my problem on that issue, but I suspect it will just confuse the issue.

  3. seven-phases-max commented on Nov 16, 2017

    @seven-phases-max
    Member

    Closing in favour of discussion at less/less-meta#16 (comment). Also renaming to match the nature of the proposal (it has very few to do with "interpolation" as such).

  4. changed the title [-]interpolating mixin names[/-] [+]Indirect mixin calls / Mixin references[/+] on Nov 16, 2017
  5. deleted a comment from stale on Nov 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions