我正在尝试生成一个包含多个主题定义的css文件,但我无法按照自己想要的方式编译它。似乎我需要对获得的值进行第二次插值,而且我不确定甚至有办法实现我正在尝试的。LESS嵌套插值
这里是我到目前为止有:
@all-themes: pastel, antique, hipster;
@pastel-background: #a7b3a5;
@pastel-primary: #b4bdc0;
@antique-background: #856357;
@antique-primary: #eae3ea;
@hipster-background: #1a2930;
@hipster-primary: #f7ce3e;
.generate-themes(@i:1) when (@i <= length(@all-themes)) {
@theme-name: extract(@all-themes, @i);
@background: ~'@@{theme-name}-background';
@primary: ~'@@{theme-name}-primary';
[email protected]{theme-name} {
background-color: @background;
color: @primary;
}
// ... more definitions ...
.generate-themes(@i + 1);
}
// call the mixin
.generate-themes();
我期待它产生如下:
body.theme-pastel {
background-color: #a7b3a5;
color: #b4bdc0;
}
body.theme-antique {
background-color: #856357;
color: #eae3ea;
}
body.theme-hipster {
background-color: #1a2930;
color: #f7ce3e;
}
不过,我进入我的CSS是这样的:
body.theme-pastel {
background-color: @pastel-background;
color: @pastel-primary;
}
body.theme-antique {
background-color: @antique-background;
color: @antique-primary;
}
body.theme-hipster {
background-color: @hipster-background;
color: @hipster-primary;
}
......这显然不起作用。有没有什么好的方法来获得“双重插值”?
你需要像'〜'@ {@ {theme-name} -background}''而不是'〜'@@ {theme-name} -background''来使用它。如果没有第二组''}','@ {theme-name}'会被编译器识别为一个变量,并将被替换为值,但其余部分(开始处的@和开始处的'-background'处结束)它只是字符串连接。编译器不会进一步评估变量,因为它没有被告知。所以,你只会得到像'@ pastel-background'这样的输出。也就是说,你的解决方法看起来比所涉及的片段更清晰。 – Harry
@哈里,如果你想发表你的评论作为答案,我会很乐意选择它作为正确的。虽然我最终重构了我的解决方法的格式,但您的答案确实提供了一种嵌套插值方法,并且可以回答原始查询。 – pulse0ne
肯定会做。我现在远离我的电脑。将在一两个小时内完成。 – Harry