question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Defining variables with a variable name?

See original GitHub issue

All of LESS documentation is pretty straightforward except for this:

@fnord: "I am fnord.";
@var: 'fnord';
content: @@var;

This apparently compiles to: content: "I am fnord.";

The example is extremely confusing because of the 3 references to the word ‘fnord’. Just what is referencing what? Can this example be rewritten with less ambiguous naming? It looks like a promising feature, but I’ve never used it because I never understood what this piece meant.

Issue Analytics

  • State:closed
  • Created 12 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cloudheadcommented, Dec 18, 2011

You cannot use + for strings, but you can do this:

.my_function(@flip: 1) {
     @somevar-1: "uppercase";
     @somevar-2: "normal";

     @var: "somevar-@{flip}";
     text-transform: @@var; /* takes the value of @somevar-1 if @flip = 1*/
}
0reactions
chrisuicommented, Dec 1, 2011

Sorry for late reply.

In theory yes. It evaluates the first variable first and then evaluates that as a variable.

What you are trying to do in your example above is concatenate two variables which the LESS language doesn’t support as far as I’m aware. You may be able to use the + operator to join strings though (not tested (or researched)):

.my_function(@flip: 1) {
    @somevar-1: uppercase;
    @somevar-2: normal;

    @var: @somevar + '-' + @flip;
    text-transform: @var;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using a string variable as a variable name - Stack Overflow
I have a variable with a string assigned to it and I want to define a new variable based on that string. foo...
Read more >
Variables in C - GeeksforGeeks
A variable in C is a memory location associated with some name in order to store some form of data and retrieve it...
Read more >
C Variables, Constants and Literals - Programiz
Rules for naming a variable · A variable name can only have letters (both uppercase and lowercase letters), digits and underscore. · The...
Read more >
Variables - The Modern JavaScript Tutorial
A variable is a “named storage” for data. We can use variables to store goodies, visitors, and other data. To create a variable...
Read more >
C# Variables: Declare, Access & Use ... - TutorialsTeacher
Above, int is a data type, num is a variable name (identifier). The = operator is used to assign a value to a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found