Search code examples
csslesslessphp

CSS LESSphp variable with CSS code block


I would like to add multiple style declaration using lessphp. I have provide the $context array with the lessphp setVariables method.

$context['font'] = 'color: #ffffff; font-family: verdana;';

Then I would like to use it in the less file with the variable name, like this:

.selector{
  @font;
}

This doesn't work, what should be the appropriate way of handle this?


Solution

  • There are at least two issues with what you are trying to do based on both LESSphp and LESS syntax:

    1. The array from php needs to be pairs of variable names and values, not property names and values.
    2. LESS does not allow dynamic setting of property names.

    So I believe a solution for you is something like this:

    PHP

    $context = array(
       "fontColor" => "#ffffff",
       "fontFamily" => "verdana"
    );
    
    $less->setVariables($context);
    

    LESS

    The above code should generate in the compiled LESS file the equivalent of this:

    @fontColor: #ffffff;
    @fontFamily: verdana;
    

    Which then you use like this:

    .selector {
       color: @fontColor;
       font-family: @fontFamily;
    }