lessphp - returning variables with a function

156 views Asked by At

When I use default less.js i can return a variable within a function like:

.function(@x,@y) {

  .innerFunction() when (@x = @y) {
    @output: @x
  }

  .innerFunction() when not (@x = @y) {
    @output: @x/10;
  }

  .innerFunction() when (...) {
    @output: @x + @y;
  }

  property: @output;

}

This is very practical to create more complex less functions but it does not work with lessphp... is there a way to return variables in lessphp?

1

There are 1 answers

0
Martin Turjak On

Lessphp doesn't permit variables to leak out of the scope of the rules where they are defined.

The lessphp documentation says this:

Variables are only visible for use from their current scope, or any enclosed scopes.

The way you could reformat a mixin like this to work in both less.js and lessphp could be something along these lines:

.function(@x,@y) {
  .innerFunction() when (@x = @y) {
    .output(@x);
  }
  .innerFunction() when not (@x = @y) {
    .output(@x/10);
  }
  .output(@output) {
    property: @output;
  };
  .innerFunction();
}

and for example calling the mixin like this in LESS:

.test-class {
  .function(20,11);
}

will give you the following CSS:

.test-class {
  property: 2;
}

There are of course many different ways how to construct more complex mixins but the solution/approach will depend on what exactly you want to achieve in the specific case.