changing dotless parameters dynamically

假装没事ソ 提交于 2019-12-12 10:55:19

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!