Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with including variables #850

Open
jgcbrouns opened this issue May 11, 2017 · 3 comments
Open

Problem with including variables #850

jgcbrouns opened this issue May 11, 2017 · 3 comments

Comments

@jgcbrouns
Copy link

Hello everybody,

First let me explain what I want to do:
In php I have variables. i.e. $variable1 = 'red';
These variables I want to inject into the .less files for compilation: @button-top-top-color: $variable1
and in the CSS this would yield: @button-top-top-color: 'red';

I am having troubles with including variables into .less files that I can subsequently convert into .css by using assetic. I found the $vars array that is a parameter in the FileAsset class. I assume this parameter can be used to set additional variables that will be injected into assetic when dumping?

I used this code to test it, but I got the following error: Uncaught RuntimeException: The asset target path "_test_1" must contain the variable "{color}"

use Assetic\Asset\AssetCollection;
use Assetic\Asset\FileAsset;
use Assetic\Filter\LessFilter;

// Play here
$variables = ['color'];
$fileAsset = new FileAsset(__DIR__.'/test.less', [new LessFilter()], null, null, $variables);
$fileAsset->setValues(['color' => '#000;']);

$css = new AssetCollection([
    $fileAsset,
]);

print_r($css->dump());

Another idea to solve my problem would be to create a StringAsset that holds the .less content with its variables. This string I would have parsed beforehand. Would this work?

Kind regards, Jeroen

@aik099
Copy link

aik099 commented May 11, 2017

I've also thought that $vars is something replaced in CSS before passing to filters, but after looking in the code this is somehow used to build final asset name. I wasn't able however find any examples of that.

@isometriks
Copy link

isometriks commented May 11, 2017

$less = new LessFilter();
$less->addParserOption('globalVars', ['color' => '#000']);

I believe the variables you're setting are meant to be used for things like i18n to generate multiple asset names / urls. Each filter handles their variables differently. Pretty sure Less just inserts your variables at the end of the file on its own before parsing.

Edit: http://lesscss.org/usage/#using-less-in-the-browser-options

globalVars adds to beginning so it won't overwrite.
modifyVars adds to the end so it overrides variables.

@jgcbrouns
Copy link
Author

Thank you very much for responding aik099 and isometriks. I managed to get it to work by using your sample code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants