3

I'm getting a syntax error when attempting to reference a variable from a namespace in LESS:

#testns {
    @my_color: #04ffff;
    .me() {
        color: blue;
    }
}

.fun {
    color: #testns[@my_color];
}

The mixin works fine if I do the following:

.fun {
    #testns > .me();
}

But I'm not able to reference the variable from the namespace for some reason. I'm building these in Java using the asual library, and everything else is working perfectly.

2
  • I don't think that's how namespaces work. You can set local variables in the namespace and reference them in mixin rules, but you can't access the variables directly (AFAIK) Commented Dec 28, 2011 at 16:21
  • @Mathletics: thanks for this comment, it helped me. Why don't you post it as an answer? Commented Jan 25, 2012 at 8:49

1 Answer 1

5

This isn't how namespaces work. Namespaces allow you to declare local-scope variables to use in mixins (sort of like a CSS closure, ha!), and the mixins can be referenced from outside the namespace bundle. The variable inside the namespace are private, however, and cannot be referenced from outside the namespace.

Sign up to request clarification or add additional context in comments.

3 Comments

From lesscss.org: “Sometimes, you may want to group your variables or mixins, for organizational purposes…” perhaps they should make the text a bit less ambiguous.
@zanona you are absolutely right. They do not explicitly state that variables aren't accessible from outside, though you could infer it from the next section on scoping. The parser doesn't actually know the difference between an id selector declaration and a #namespace block, except that the latter has no declared rules, only variables and mixins. So, at least right now, variables are trapped in their scope.
Docs have been updated to state how this works more clearly: github.com/less/less-docs/pull/154/files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.