我正在嘗試生成一個包含多個主題定義的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