是否可以使用插值字符串引用变量?

发布于 2024-11-07 12:44:10 字数 426 浏览 1 评论 0原文

我有一个类别列表,我想从中设置背景颜色。我想将背景颜色的值保留为变量。是否可以通过字符串插值引用变量? Sass 向我当前使用此代码抛出“无效 CSS”错误:

/* Category Colors */
$family_wellness_color: #c1d72e;
$lifestyle_color: #f4eb97;
$food_color: #f78f1e;
...

/* Categories */
@each $cat in family_wellness, lifestyle, food
{
    .#{$cat}
    {
        .swatch, .bar
        {
            background-color: $#{$cat}_color;
        }
    }
}

可能吗?我真的很感激一些建议!

I have a list of categories, from which I would like to set a background-color. I would like to keep the values for the background colors as variables. Is it possible to reference a variable by string interpolation? Sass is throwing an "Invalid CSS" error on me currently using this code:

/* Category Colors */
$family_wellness_color: #c1d72e;
$lifestyle_color: #f4eb97;
$food_color: #f78f1e;
...

/* Categories */
@each $cat in family_wellness, lifestyle, food
{
    .#{$cat}
    {
        .swatch, .bar
        {
            background-color: $#{$cat}_color;
        }
    }
}

Possible? I would really appreciate some advice!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

断肠人 2024-11-14 12:44:10

好吧,我能得到的最接近我想要的东西是:

#_variables.scss
/* Categories */
$family_wellness_color: #c1d72e;
$lifestyle_color: #f4eb97;
$food_color: #f78f1e;
$media_entertainment_color: #db3535;
$travel_recreation_color: #e30e61;
$education_color: #92278f;
$sports_color: #0070bb;
$technology_color: #00b5cc;
$products_shopping_color: #028e99;
$companies_businesses_color: #56BA42;

#_mixins.scss
@import 'variables';

@mixin get_category_bkgd_color($category_name)
{
    @if $category_name == family_wellness
    {
        @include bkgd_color($family_wellness_color);
    }
    @else if $category_name == lifestyle
    {
        @include bkgd_color($lifestyle_color);
    }
    @else if $category_name == food
    {
        @include bkgd_color($food_color);
    }
    @else if $category_name == media_entertainment
    {
        @include bkgd_color($media_entertainment_color);
    }
    @else if $category_name == travel_recreation
    {
        @include bkgd_color($travel_recreation_color);
    }
    @else if $category_name == education
    {
        @include bkgd_color($education_color);
    }
    @else if $category_name == sports
    {
        @include bkgd_color($sports_color);
    }
    @else if $category_name == technology
    {
        @include bkgd_color($technology_color);
    }
    @else if $category_name == products_shopping
    {
        @include bkgd_color($products_shopping_color);
    }
    @else if $category_name == companies_businesses
    {
        @include bkgd_color($companies_businesses_color);
    }
}

#dashboard.scss
@import 'variables', 'mixins';

@each $cat in family_wellness, lifestyle, food, media_entertainment, travel_recreation, education, sports, technology, products_shopping, companies_businesses
{
    .#{$cat}
    {
        .swatch, .bar
        {
            @include get_category_bkgd_color($cat);
        }
    }
}

不是最优雅的解决方案,但它确实为我提供了一个可以在其他几个领域重复使用的 mixin。有谁找到一种方法可以提高效率?

Well, the closest I could get to what I wanted was:

#_variables.scss
/* Categories */
$family_wellness_color: #c1d72e;
$lifestyle_color: #f4eb97;
$food_color: #f78f1e;
$media_entertainment_color: #db3535;
$travel_recreation_color: #e30e61;
$education_color: #92278f;
$sports_color: #0070bb;
$technology_color: #00b5cc;
$products_shopping_color: #028e99;
$companies_businesses_color: #56BA42;

#_mixins.scss
@import 'variables';

@mixin get_category_bkgd_color($category_name)
{
    @if $category_name == family_wellness
    {
        @include bkgd_color($family_wellness_color);
    }
    @else if $category_name == lifestyle
    {
        @include bkgd_color($lifestyle_color);
    }
    @else if $category_name == food
    {
        @include bkgd_color($food_color);
    }
    @else if $category_name == media_entertainment
    {
        @include bkgd_color($media_entertainment_color);
    }
    @else if $category_name == travel_recreation
    {
        @include bkgd_color($travel_recreation_color);
    }
    @else if $category_name == education
    {
        @include bkgd_color($education_color);
    }
    @else if $category_name == sports
    {
        @include bkgd_color($sports_color);
    }
    @else if $category_name == technology
    {
        @include bkgd_color($technology_color);
    }
    @else if $category_name == products_shopping
    {
        @include bkgd_color($products_shopping_color);
    }
    @else if $category_name == companies_businesses
    {
        @include bkgd_color($companies_businesses_color);
    }
}

#dashboard.scss
@import 'variables', 'mixins';

@each $cat in family_wellness, lifestyle, food, media_entertainment, travel_recreation, education, sports, technology, products_shopping, companies_businesses
{
    .#{$cat}
    {
        .swatch, .bar
        {
            @include get_category_bkgd_color($cat);
        }
    }
}

Not the most elegant solution, but it does get me a mixin I can re-use in several other areas. Does anyone see a way to make this more efficient?

二智少女猫性小仙女 2024-11-14 12:44:10

使用 Rails 3.1,您可以创建如下模板:screen.css.scss.erb - 它具有 scss 和 erb 的所有优点。

<% [...].each do |category_name| %>
  @include bkgd_color(
lt;%= category_name %>_color);
<% end %>

With Rails 3.1, you can create templates like this: screen.css.scss.erb - it comes with all the goodness of scss and erb.

<% [...].each do |category_name| %>
  @include bkgd_color(
lt;%= category_name %>_color);
<% end %>
云归处 2024-11-14 12:44:10

.html:

<ul>
    <li class="family"></li>
    <li class="life"></li>
    <li class="food"></li>
</ul>

.scss:

$family_color: #c1d72e;
$life_color: #f4eb97;
$food_color: #f78f1e;

// solution 1 - using direct (key, value) pair
@each $cat, $cat_var in (family, $family_color), (life, $life_color), (food, $food_color) {
    .#{$cat} {
        background-color: $cat_var;
    }
}

// solution 2 - using nth function 
@each $cat in (
    family $family_color,
    life $life_color,
    food $food_color) {
      .#{nth($cat, 1)} {
          background-color: nth($cat, 2);
      }
}

// solution 3 - using sass map
$colors: (
    family_color: #c1d72e,
    life_color: #f4eb97,
    food_color: #f78f1e
);

@function color($key){
    @if map-has-key($colors, $key){
        @return map-get($colors, $key);
    }
}

@each $color in family, life, food{
    .#{$color}{
        background-color: color(#{$color}_color);
    }
}

.html:

<ul>
    <li class="family"></li>
    <li class="life"></li>
    <li class="food"></li>
</ul>

.scss:

$family_color: #c1d72e;
$life_color: #f4eb97;
$food_color: #f78f1e;

// solution 1 - using direct (key, value) pair
@each $cat, $cat_var in (family, $family_color), (life, $life_color), (food, $food_color) {
    .#{$cat} {
        background-color: $cat_var;
    }
}

// solution 2 - using nth function 
@each $cat in (
    family $family_color,
    life $life_color,
    food $food_color) {
      .#{nth($cat, 1)} {
          background-color: nth($cat, 2);
      }
}

// solution 3 - using sass map
$colors: (
    family_color: #c1d72e,
    life_color: #f4eb97,
    food_color: #f78f1e
);

@function color($key){
    @if map-has-key($colors, $key){
        @return map-get($colors, $key);
    }
}

@each $color in family, life, food{
    .#{$color}{
        background-color: color(#{$color}_color);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文