如何在 SASS 中引用字符串?

发布于 2024-08-04 05:42:14 字数 511 浏览 8 评论 0原文

我正在使用 SASS 生成 @font-face mixin,但是这样:

=remotefont(!name, !url)
  @font-face
    font-family = !name
    src = url(!url + ".eot")
    src = local(!name), url(!url + ".ttf") format("truetype")

+remotefont("My font", "/myfont.ttf")

变成这样:

@font-face {
  font-family: My font;
  src: url(/myfont.ttf.eot);
  src: local(My font), url(/myfont.ttf.ttf) format(truetype); }

无论我如何尝试,我都不能让它引用“我的字体”(带有“!name”)或“truetype” (它删除了引号)。关于我如何做到这一点有什么想法吗?

I'm using SASS to generate a @font-face mixin, however this:

=remotefont(!name, !url)
  @font-face
    font-family = !name
    src = url(!url + ".eot")
    src = local(!name), url(!url + ".ttf") format("truetype")

+remotefont("My font", "/myfont.ttf")

becomes this:

@font-face {
  font-family: My font;
  src: url(/myfont.ttf.eot);
  src: local(My font), url(/myfont.ttf.ttf) format(truetype); }

No matter how much I try, I can't have it quote either "My font" (with "!name") or "truetype" (it removes the quotes). Any ideas on how I can do this?

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

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

发布评论

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

评论(7

挽清梦 2024-08-11 05:42:14

使用字符串插值可以做得更好一点:

!name = "asdf"
.foo
  font-family = "\"#{!name}\""

但我同意我们需要一种更好的方法来处理 sass 中带引号的字符串。 Sass 有足够的上下文来执行一些智能操作,而不是将引用逻辑卸载给用户。

It can be made a little better using string interpolation:

!name = "asdf"
.foo
  font-family = "\"#{!name}\""

But I agree that we need a better approach for dealing with quoted strings in sass. Sass has enough context to do something intelligent here and not offload the quoting logic to the user.

欲拥i 2024-08-11 05:42:14

您可以在变量中引用,在双引号内使用单引号。这就是我的做法:

!string = "'Myriad Pro', 'Lucida Sans', Helvetica, Arial, sans-serif"
.foo
  :font-family= !string

这将正确编译为:

.foo{
  font-family: 'Myriad Pro', 'Lucida Sans', Helvetica, Arial, sans-serif; }

我认为你不能以相反的方式引用(即单引号内的双引号)。这样做会给你带来编译错误。

希望有帮助!

You can quote in your variables, use single quotes inside double quotes. This is how I do it:

!string = "'Myriad Pro', 'Lucida Sans', Helvetica, Arial, sans-serif"
.foo
  :font-family= !string

This will compile correctly to:

.foo{
  font-family: 'Myriad Pro', 'Lucida Sans', Helvetica, Arial, sans-serif; }

I think you can't quote the other way round (i.e. double quotes inside single quotes). Doing that will give you compile errors.

Hope that helped!

苏佲洛 2024-08-11 05:42:14

好吧,我发现我需要做:

"\"" + !name + "\""

该死,这是一些尴尬的语法......

Okay, I found that I need to do:

"\"" + !name + "\""

Damn that's some awkward syntax...

水染的天色ゝ 2024-08-11 05:42:14

使用 http://www.fontsquirrel.com/fontface/generator
我有一个相应的 Sass mixin:

=addfont(!name, !url, !family = 0)
  @if !family == 0
    !family = !name
  @font-face
    font-family = "'#{!name}'"
    src = url(!url + ".eot")
    src = local("'#{!name}'"), local("'#{!family}'"), url(!url + ".woff") format("'woff'"), url(!url + ".ttf") format("'truetype'"), url(!url + ".svg#" + !name) format("'svg'")

Using http://www.fontsquirrel.com/fontface/generator
I have a corresponding Sass mixin:

=addfont(!name, !url, !family = 0)
  @if !family == 0
    !family = !name
  @font-face
    font-family = "'#{!name}'"
    src = url(!url + ".eot")
    src = local("'#{!name}'"), local("'#{!family}'"), url(!url + ".woff") format("'woff'"), url(!url + ".ttf") format("'truetype'"), url(!url + ".svg#" + !name) format("'svg'")
烦人精 2024-08-11 05:42:14

这会在事物周围加上引号:

@font-face {
  src: url("\"#{$ngx-font-path}/ngx-icon.eot\"");
}

This puts quotes around things:

@font-face {
  src: url("\"#{$ngx-font-path}/ngx-icon.eot\"");
}
属性 2024-08-11 05:42:14

您可以使用字符串函数 quote

font-family: quote(!name)

You can use the string function quote:

font-family: quote(!name)
后eg是否自 2024-08-11 05:42:14

转义引号的一个简单方法是使用 SASS inspect 函数。这是 Bootstrap 插入字体变量的方式,并允许您使用包含引号的字符串,而不是将整个字符串括在引号中。

$font-family-sans-serif: system-ui, -apple-system, "Segoe UI", Roboto;

--my-escaped-font-family: #{inspect($font-family-sans-serif)};

A simple way to escape the quotes is to use the SASS inspect function. This is the way that Bootstrap interpolates font variables and allows you to use a string that includes quotes instead of wrapping the whole string in quotes.

$font-family-sans-serif: system-ui, -apple-system, "Segoe UI", Roboto;

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