如何在 SASS 中引用字符串?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用字符串插值可以做得更好一点:
但我同意我们需要一种更好的方法来处理 sass 中带引号的字符串。 Sass 有足够的上下文来执行一些智能操作,而不是将引用逻辑卸载给用户。
It can be made a little better using string interpolation:
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.
您可以在变量中引用,在双引号内使用单引号。这就是我的做法:
这将正确编译为:
我认为你不能以相反的方式引用(即单引号内的双引号)。这样做会给你带来编译错误。
希望有帮助!
You can quote in your variables, use single quotes inside double quotes. This is how I do it:
This will compile correctly to:
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!
好吧,我发现我需要做:
该死,这是一些尴尬的语法......
Okay, I found that I need to do:
Damn that's some awkward syntax...
使用 http://www.fontsquirrel.com/fontface/generator
我有一个相应的 Sass mixin:
Using http://www.fontsquirrel.com/fontface/generator
I have a corresponding Sass mixin:
这会在事物周围加上引号:
This puts quotes around things:
您可以使用字符串函数 quote:
You can use the string function quote:
转义引号的一个简单方法是使用 SASS inspect 函数。这是 Bootstrap 插入字体变量的方式,并允许您使用包含引号的字符串,而不是将整个字符串括在引号中。
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.