为什么不创建一个具有执行所需查找功能的自定义 .LESS 插件?
下面的代码经过测试,如图所示。我实际上并没有在数据库中查找数据,但所有必要的信息都应该可用。我验证了在网站上以 Windows 身份验证模式运行时,我能够通过HttpContext.Current.User.Identity.Name 检索当前用户。
要使用下面的函数,你可以在你的 less 文件中输入如下内容:
--lookup using custom function (hit db)
@brand_color:getCustomColor(usersThemeAttribute);
--then use the variable like normal
color: @brand_color;
代码
[DisplayName("UserProfilePlugin")]
public class UserProfilePlugin : IFunctionPlugin
{
public Dictionary<string, Type> GetFunctions()
{
return new Dictionary<string, Type>
{
{ "getCustomColor", typeof(GetCustomColorFunction) }
};
}
}
public class GetCustomColorFunction : Function
{
protected override Node Evaluate(Env env)
{
Guard.ExpectNumArguments(1, Arguments.Count(), this, Location);
Guard.ExpectNode<Keyword>(Arguments[0], this, Arguments[0].Location);
//the idea is that you would have many colors in a theme, this would be the name for a given color like 'bgColor', or 'foreColor'
var colorAttrName = Arguments[0] as Keyword;
//Lookup username
// string username = HttpContext.Current.User.Identity.Name;
//perform some kind of DB lookup using the username, get user theme info
// UserProfile up = new UserProfile();
// System.Drawing.Color c = up.GetColorByAttribute(colorAttrName.Value);
//return the appropriate color using RGB/etc...
// return new Color(c.R, c.G, c.B);
return new Color(129, 129, 129);
}
}
要注册插件,请将其添加到 web.config:
<dotless cache="false" >
<plugin name="UserProfilePlugin" assembly="Your.Assebly.Name" />
</dotless>
考虑为 dotless 禁用缓存,以便用户所做的更改立即生效。
链接:https://github.com/dotless/dotless/wiki/FunctionPlugins