简体   繁体   English

DOTLESS - 用户特定变量

[英]DOTLESS - user specific variables

I am writing an ASP.NET MVC application and I plan to use LESS for stylesheets using the Dotless compiler. 我正在编写一个ASP.NET MVC应用程序,我打算使用无点编译器将LESS用于样式表。 I want to make my application skinnable and found that I can use LESS variables to customize the styles. 我想让我的应用程序换肤,并发现我可以使用LESS变量来自定义样式。

How do I best implement this? 我该如何最好地实现这一点?

I want to be able to override the variables in nested hierarchies. 我希望能够覆盖嵌套层次结构中的变量。 I have a site wide variables.less which contains all variables. 我有一个包含所有变量的站点范围变量。 I now want to allow each group of customers to have a their specific overrides. 我现在想让每组客户都有一个特定的覆盖。 Then I want each customer to have their own overrides, but with the "group defaults" if some variables are not defined. 然后我希望每个客户都有自己的覆盖,但如果没有定义某些变量,则使用“组默认值”。 Then I want "user" overrides (there are several users per customer). 然后我想要“用户”覆盖(每个客户有几个用户)。

One alternative I thought of was that I create a variables.default.less file which defines all variables. 我想到的另一种选择是我创建了一个定义所有变量的variables.default.less文件。 I then create one "override" file redefining only the changed variables. 然后我创建一个“覆盖”文件,仅重新定义已更改的变量。 This allows me to create a user specific less file to include all nested variable overrides. 这允许我创建一个特定于用户的文件,以包含所有嵌套的变量覆盖。

Examples: 例子:

variables.default.less variables.default.less

@bgcolor: #ffffff;
@textcolor: #000000;
@fontsize: 12px;
@logo: "site-default.png";

variables.customergroup01.less variables.customergroup01.less

@bgcolor: #cccccc;
@textcolor: #999999;

variables.customer99.less variables.customer99.less

@logo: "customer-logo99.png";

variables.user1234.less variables.user1234.less

@font-size: 18px;

Now if customer 55 logs in (he belongs to customergroup01), he gets the following stylesheet 现在,如果客户55登录(他属于customergroup01),他将获得以下样式表

// Import default vars
@import "variables.default.less";

// Import customized vars
@import "variables.customergroup01.less";

// This is the actual stylesheet
@import "styles.less"; 

Now if user 1234 (customer 99 and customergroup01), he gets the following stylesheet 现在,如果用户1234(客户99和customergroup01),他将获得以下样式表

// Import default vars
@import "variables.default.less";

// Import customized vars
@import "variables.customergroup01.less";
@import "variables.customer99.less";
@import "variables.user1234.less";

// This is the actual stylesheet
@import "styles.less"; 

Is this a usable pattern? 这是一个可用的模式吗? Do I render the customized less files on the fly or create somehow precompile them? 我是否动态渲染自定义的较少文件或以某种方式预编译它们?

Thank you! 谢谢!

Is this a usable pattern? 这是一个可用的模式吗?

I would say so yes. 我会说是的。 Your requirement is to create different stylesheets based on a user and this seems a sensible approach. 您的要求是根据用户创建不同的样式表,这似乎是一种明智的方法。 My implementation is slightly different. 我的实现略有不同。 I generate a " base " stylesheet that contains all the core styles and then create a separate " theme " stylesheet that contains the customised styles (and these styles have their own classes prefixed t- ). 我生成一个包含所有核心样式的“ 基础 ”样式表,然后创建一个单独的“ 主题 ”样式表,其中包含自定义样式(这些样式有自己的类前缀为t- )。

Example

Site.less Site.less

@import "variables.less";
@import "styles.less"; 

Theme.less Theme.less

@import "theme-variables.less";

// theme class
.t-btn-submit {
    background-colour: @t-btn-submit-bg;
}

This approach gives me a nice separation of concerns between the changeable styles and the core styling. 这种方法让我在可变样式和核心样式之间进行了很好的分离。 I know that if I change the core styling (maybe remove classes) it won't break the theme as they have separate classes. 我知道如果我改变核心样式(可能删除类),它将不会破坏主题,因为它们有单独的类。 It also simplifies the process and makes it more manageable when updating or changing. 它还简化了流程,使其在更新或更改时更易于管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM