问题
I would like to store .less parameters in a DB, an example use case might be that colour changes for each user based on his or her preference.
I figure the best way to do this would be to parse all the .less files to get the parameters, the user can then set the parameters which would save in a DB against the user. When the less file is requested a handler overload can query the DB for the parameters. The handler can then output the css with the parameters set.
Questions: - How can I take a .less file and get a list of parameters? - How can I take a .less file an array of parameters (key value pairs) and output css?
All using the dotless framework.
回答1:
Might be a late reply but we did something similar to your requirement, where we had custom colors based on users stored in the database.
Our solution was adding custom code to the source of dotless which is available here I believe https://github.com/dotless/dotless
So then during the parsing it justs string replaced the parts we wanted to replace. Only drawback to this approach is to get updated builds of the new dotless dll we need to remerge our source each time.
EDIT Here is an example code snippet:
dotless.Core.Utils.HslColor hslcolor = dotless.Core.Utils.HslColor.FromRgbColor( new dotless.Core.Parser.Tree.Color( "187AAB" );
hslcolor.Lightness = 0.93;
var hexString = '#' + ( hslcolor.ToRgbColor().RGB.Select( i => ( ( int )i ).ToString( "X2" ) ).Aggregate( ( a, b ) => a + b ) ).ToLowerInvariant();
var resultColor = hexString;
回答2:
If you are doing it in .Net then dotless allows you to specify a plugin which can be a visitor to run before evaluation. This visits all the nodes in the less abstract syntax tree so could easily determine all the variables. Dotless also allows specifying patamteres allowing you to generate a variables file.
But that is ott.. you might want to think about storing all variables in the database as your "master copy" and avoid having to parse the less.
来源:https://stackoverflow.com/questions/10797661/changing-dotless-parameters-dynamically