Ignore parts of sass file for parsing

◇◆丶佛笑我妖孽 提交于 2019-12-05 22:19:43

问题


I'm trying to get SASS to ignore part of my .scss code while parsing.

It's for a shopify theme that I'm making. My scss files are compiled into .css.liquid files so that I can put shopify's templating language liquid into the css too.

I want the compiled css.liquid to look like this:

.header--logo {
  background: url({{ 'header-logo.png' | asset_url }}) 0 0 no-repeat;
}

In LESS I used to do this:

.header--logo {
  background: ~"{{ 'header-logo.png' | asset_url }}" 0 0 no-repeat;
}

Does SASS also have an easy way to do the same?

Thanks!


回答1:


Sass doesn't provide an escape-all-the-declaration feature.

To pass a CSS value as-is, without being parsed with Sass, put it into quotes and use the unquote() function to remove the quotes:

.header--logo {
  background: unquote("url({{ 'header-logo.png' | asset_url }}) 0 0 no-repeat");
}

If you use that very often, you can make it a tad easier with a helper function:

@function i($str) {
  @return unquote($str);
}

.header--logo {
  background: i("url({{ 'header-logo.png' | asset_url }}) 0 0 no-repeat");
}

Alternatively, you can "igonre" a part of the value rather than the whole value. You'll need interpolation to do this, i. e. #{unquote('...')}. Example usage:

.header--logo {
  background: url(#{unquote("{{ 'header-logo.png' | asset_url }}")}) 0 0 no-repeat;
}

Demo of all approaches: http://sassmeister.com/gist/4920a805f0451192ec9b



来源:https://stackoverflow.com/questions/25136523/ignore-parts-of-sass-file-for-parsing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!